Hello everyone! The Brain Teaser series continues.

First, we would like to announce the winner of the previous brain teaser. We got a number of very interesting and creative solutions. The shortest answer — [Net.DNS]::GetHostAddresses(“”), with 31 characters — was given by *sahal*. So, he is the winner!

Congratulations sahal, you get an eBook version of Microsoft Windows PowerShell 3.0 First Look written by Adam Driscoll and published by Packt.

We would like to thank Joel ‘Jaykul’ Bennett for the new brain teaser.

You have an array of words: ‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’. Your new task is to output only palindromic words.

The input is: ‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’

The output is:

Hannah

12321

Madam

Once again, the shortest answer wins! Be aware, a space is a character too.

Please use the comment box at the bottom of this page to submit your solutions by Friday. The winner will be announced on the next Monday.

Don’t have a solution of your own or has it already been posted by others? You can still participate and add your voice by voting on the existing comment by using the up/down voting arrows.

This time, the prize is again the eBook version of Microsoft Windows PowerShell 3.0 First Look written by Adam Driscoll. We would like to thank our sponsor Packt, one of the most prolific and fast-growing tech book publishers in the world, for providing such a cool prize.

Good luck!

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{[string]::Join(“”,$_[-1..-$_.Length])-eq$_}

NICE Mike! Much simpler than mine…

$input|?{!($_|%{$u=$_;$i=$u.length-1;0..($i/2)|?{$u[$_]-ne$u[$i-$_]}})}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{$_-eq(-join$_[-1..-$_.Length])}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{$_-eq-join$_[-1..-$_.Length]}

$OFS=”;’Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{“$($_[-1..-$_.Length])”-eq$_}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{$OFS=”;”$($_[-1..-$_.Length])”-eq$_}

Also works if you wanted to self-contain the $OFS=”

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{-join$_[-1..-$_.Length]-eq$_}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{-join$_[($_.Length-1)..0]-eq$_}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{-join$_[-1..-50]-eq$_}

Turns out you don’t need the exact length of the string.

It doesn’t work for more than 50 characters: ‘ABCDEFGHIJKLMNOPQRSTUVWXYZZYXWVUTSRQPONMLKJIHGFEDCBA’|?{-join$_[-1..-50]-eq$_}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{-join$_[-1..-189819]-eq$_}

That should be able to cover the longest know word in the English language. The description says we have an array of words. It is incredibly slow but you still don’t have to use the exact length. “-189819” is still shorter than “$_.Length”. (http://en.wikipedia.org/wiki/Longest_word_in_English) 🙂

based on the description, this also works:

Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{-join$_[5..0]-eq$_}

Different approach:

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{[array]::Reverse(($X=$_.ToCharArray()));-join$x-eq$_}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{-join$_[$_.Length..0]-eq$_}

$words|?{$_-eq-join[regex]::Matches($_,”.”,’RightToLeft’)}

‘Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’|?{$_-eq-join$_[-1..-6]}

(’Hannah’,’Jeffrey’,’12321′,’Madam’,’Abracadabra’)[0,2,3] 😀