Tuesday, January 19, 2016

Big hint to both PowerShell riddles

PowerShell riddle 1

If ( $x.$x($x,$x–eq $x ) { <# What is $x? #> }

It is a true equality; no type conversion is performed for the comparison.

PowerShell riddle 2

If ( $y.$y($y,$y.$y($y)) -eq 0 ) { <# What is $y? #> }

Again, it is a true equality; no type conversion is performed for the comparison.

Big hint

In PowerShell, when calling a method or property of an object, the name of the method or property can be represented by any expression that evaluates to be the name of the method or property.

These all result in 3.

@( 1, 2, 3 ).Count
@( 1, 2, 3 ).'Count'
@( 1, 2, 3 ).$( [char]67 + 'ou' + "NT".ToLower() )


Answer to riddle 1, which is a hint to riddle 2.

PowerShell riddles

PowerShell riddle 1

If ( $x.$x($x,$x–eq $x ) { <# What is $x? #> }

It is a true equality; no type conversion is performed for the comparison.

PowerShell riddle 2

If ( $y.$y($y,$y.$y($y)) -eq 0 ) { <# What is $y? #> }

Again, it is a true equality; no type conversion is performed for the comparison.


Big hint to both riddles.

Answer to PowerShell riddle 1

Spoilers below! Obviously. Go here for the posing of the two riddles.



Riddle 1

If ( $x.$x($x,$x–eq $x ) { <# What is $x? #> }

It is a true equality; no type conversion is performed for the comparison.

Answer to riddle 1

In PowerShell, when calling a method or property of an object, the name of the method or property can be represented by any expression that evaluates to be the name of the method or property.

$x is a string and the name of a string method. It must be a method that can take two string parameters. And we are looking specifically for one that would leave $x essentially unchanged.

$x = 'Replace'

If we replace the string ‘Replace’ with the string ‘Replace’ anywhere it appears in the string ‘Replace’, we get the string ‘Replace’.

'Replace'.Replace( 'Replace', 'Replace' ) -eq 'Replace'

Riddle 2

So with that as big hint, can you figure out the second riddle?

If ( $y.$y($y,$y.$y($y)) -eq 0 ) { <# What is $y? #> }

Again, it is a true equality; no type conversion is performed for the comparison.

Answer to riddle 2

Answer to PowerShell riddle 2

Spoilers below! Obviously. Go here for the posing of the two riddles.



Riddle 2

If ( $y.$y($y,$y.$y($y)) -eq 0 ) { <# What is $y? #> }

It is a true equality; no type conversion is performed for the comparison.

Answer to riddle 2

In PowerShell, when calling a method or property of an object, the name of the method or property can be represented by any expression that evaluates to be the name of the method or property.

$y is a string and the name of a string method. We call it twice, so let’s simplify and pull out the nested call.

$z = $y.$y$y )
$y.$y$y, $z ) –eq 0

So we can see that it must be a method that returns a number. It must be able to take a single string as a parameter, or a string and number.

There are a couple string methods that meet those requirements, but only one that results in the full equation being true, that is, only one that results in a zero in our usage.

$y = 'IndexOf'

The index (location) of the string ‘IndexOf’ within the string ‘IndexOf’ is 0.

The index of the string ‘IndexOf’ within the string ‘IndexOf’ starting the search at location 0 is 0.

$z = 'IndexOf'.IndexOf( 'IndexOf' )
'IndexOf'.IndexOf( 'IndexOf', 0 ) –eq 0

All together:

'IndexOf'.IndexOf( 'IndexOf', 'IndexOf'.IndexOf( 'IndexOf' ) ) -eq 0