Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

##PowerShell

PowerShell

$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2

Output:

PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2

What the deuce?

Hint 1:

First, understand that the code (a, b)[c] (replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array (a, b) is indexed into based on the value c.

Hint 2:

Next, understand that $true = 1 and $false = 0 and that arrays are zero-indexed. Thus, depending upon whether $c-eq2 ("$c is equal to 2") is True or False, we'll choose either the second or first value, respectively, and add that on to the 2.

Hint 3:

The dynamic array gets calculated before indexing.

Explanation:

The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as $c and saved as 2, and then the post-increment happens and sets $c=$c+1=3. The second element is processed as $c and saved as 3 (since $c was just set to 3), and then post-decremented back to 2. This makes our array (2,3). We index into that with $c-eq2 ... well, $c is now 2 again, so that evaluates to $true, or 1, and so selects the second element of the array, 3. Thus 2+3=5. And, since $c was decremented back down, we get the bonus of being able to output c = 2 still.

##PowerShell

$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2

Output:

PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2

What the deuce?

Hint 1:

First, understand that the code (a, b)[c] (replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array (a, b) is indexed into based on the value c.

Hint 2:

Next, understand that $true = 1 and $false = 0 and that arrays are zero-indexed. Thus, depending upon whether $c-eq2 ("$c is equal to 2") is True or False, we'll choose either the second or first value, respectively, and add that on to the 2.

Hint 3:

The dynamic array gets calculated before indexing.

Explanation:

The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as $c and saved as 2, and then the post-increment happens and sets $c=$c+1=3. The second element is processed as $c and saved as 3 (since $c was just set to 3), and then post-decremented back to 2. This makes our array (2,3). We index into that with $c-eq2 ... well, $c is now 2 again, so that evaluates to $true, or 1, and so selects the second element of the array, 3. Thus 2+3=5. And, since $c was decremented back down, we get the bonus of being able to output c = 2 still.

PowerShell

$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2

Output:

PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2

What the deuce?

Hint 1:

First, understand that the code (a, b)[c] (replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array (a, b) is indexed into based on the value c.

Hint 2:

Next, understand that $true = 1 and $false = 0 and that arrays are zero-indexed. Thus, depending upon whether $c-eq2 ("$c is equal to 2") is True or False, we'll choose either the second or first value, respectively, and add that on to the 2.

Hint 3:

The dynamic array gets calculated before indexing.

Explanation:

The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as $c and saved as 2, and then the post-increment happens and sets $c=$c+1=3. The second element is processed as $c and saved as 3 (since $c was just set to 3), and then post-decremented back to 2. This makes our array (2,3). We index into that with $c-eq2 ... well, $c is now 2 again, so that evaluates to $true, or 1, and so selects the second element of the array, 3. Thus 2+3=5. And, since $c was decremented back down, we get the bonus of being able to output c = 2 still.

Source Link
AdmBorkBork
  • 43.7k
  • 5
  • 107
  • 288

##PowerShell

$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2

Output:

PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2

What the deuce?

Hint 1:

First, understand that the code (a, b)[c] (replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array (a, b) is indexed into based on the value c.

Hint 2:

Next, understand that $true = 1 and $false = 0 and that arrays are zero-indexed. Thus, depending upon whether $c-eq2 ("$c is equal to 2") is True or False, we'll choose either the second or first value, respectively, and add that on to the 2.

Hint 3:

The dynamic array gets calculated before indexing.

Explanation:

The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as $c and saved as 2, and then the post-increment happens and sets $c=$c+1=3. The second element is processed as $c and saved as 3 (since $c was just set to 3), and then post-decremented back to 2. This makes our array (2,3). We index into that with $c-eq2 ... well, $c is now 2 again, so that evaluates to $true, or 1, and so selects the second element of the array, 3. Thus 2+3=5. And, since $c was decremented back down, we get the bonus of being able to output c = 2 still.

AltStyle によって変換されたページ (->オリジナル) /