Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

(Golf) Reversal

by japhy (Canon)
on May 02, 2001 at 08:38 UTC ( #77237=perlmeditation: print w/replies, xml ) Need Help??

This looks deceptively easy. My code (which I believe is as short as possible) is 11 characters. (Perhaps this is unfair -- it's more like a "who can hit the ball where I hit it" challenge than a "who can hit the ball far" challenge.)

Goal: create a function that takes a list of strings as its arguments and returns the characters of those strings in reversed order. That's it!

print R(123); # 321 print R(12,34,56); # 654321 sub R {...} # <-- my best score: 11
More than meets the eye. I'm betting a handful of you get an answer of 14 characters, but there might be a couple who slip through and figure out the trick to save a character, and another few to get it down to 11.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: (Golf) Reversal
by MeowChow (Vicar) on May 02, 2001 at 08:55 UTC
    12 so far:
    sub R { ''.reverse@_ }
    Hmmm... here's an 11 character one that works on your examples, only because they are numbers:
    sub R { 0+reverse@_ }
                   s aamecha.s a..a\u$&owag.print
      Ok. Now come up with an 11-character one that works for non-numbers. ;)
      Hint? I'm being a bit of a wise-ass, aren't I? ;)

      japhy -- Perl and Regex Hacker

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://77237]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2018-07-16 16:11 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (344 votes). Check out past polls.