19

Output only the palindromic words

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!

Filed in: Brainteasers Tags: ,

19 Responses to "Output only the palindromic words"

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

  2. beefarino says:

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

  3. GDisk FLa says:

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

  4. GDisk FLa says:

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

  5. Matt says:

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

    • Matt says:

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

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

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

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

    • DVS says:

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

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

  8. Jaap Brasser says:

    Different approach:

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

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

  10. DVS says:

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

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

  12. Jakub Jareš says:

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

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: