Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(Golf) Friday Golf: Print Formatted Sequence of Digits

by enoch (Chaplain)
on Aug 14, 2003 at 18:49 UTC ( #283960=perlmeditation: print w/ replies, xml ) Need Help??

Over at Michael Tsai's Weblog, he's posted the following entry Today's Shell Snippets. Each snippet (jot, Python, and Perl) prints out following:
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 01 +9 020
From open single quote to close single quote, it's 58 chars. Performing standard golf practice against his method, it can be reduced down to 48.

perl -e 'print join" ",map sprintf("%03u", $_),2..20,"\n"'

Can anyone get it lower?

enoch

Comment on (Golf) Friday Golf: Print Formatted Sequence of Digits
Select or Download Code
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by ajdelore (Pilgrim) on Aug 14, 2003 at 19:10 UTC

    Well, I've never really golfed before, but I'll give it a whirl...

    # original example, 58 chars perl -e 'print join(" ", map { sprintf("%03u", $_) } (2..20)), "\n"'
    # my golf score: 45 chars perl -e 'print((map{sprintf("%03u ",$_)}(2..20)),"\n")'

    </ajdelore>

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by hardburn (Abbot) on Aug 14, 2003 at 19:14 UTC

    Slight trick to get rid of the join.

    perl -e '$,=" ";print map(sprintf("%03u",$_),2..20),"\n"'

    Saves 1 char. I feel special!

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits(25?)
by BrowserUk (Pope) on Aug 14, 2003 at 19:15 UTC

    25
    perl -e"printf'%03d 'x18 .$/,2..20"


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      Don't mind that thumping sound in the background--that's just my head slamming against the monitor, saying "Why didn't I think of that?" :)

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

      I'm sure that you meant:
      perl -e"printf'%03d 'x19 .$/,2..20"

      By the way, it's 26 unless you started counting with 0.

      25

      Have you miscounted? I count that as 26 between the quotes... you should really count your switches though. So, I'd give that a 30.

      And I'm up two strokes with 28. :-P

      (Or, we are at 28 27 and 26 25¹ repectively if we put our solutions in files to avoid problems that result from quoting on different platforms.)

      And it would be 24 but I think -l is warranted! :-)

      -sauoq
      "My two cents aren't worth a dime.";
      

      I'm not sure this is a correct solution. The original printed a newline at the end, your version prints a space.

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits (First Attempt)
by Enlil (Parson) on Aug 14, 2003 at 19:15 UTC
    perl -e 'printf("%03u ",$_) for 2..19;print"020\n"'

    for 40

    -enlil

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by sauoq (Abbot) on Aug 14, 2003 at 19:17 UTC
    perl -le 'print qq(@{["002".."020"]})'

    Update:

    perl -le'$,=$";print"002".."020"'

    -sauoq
    "My two cents aren't worth a dime.";
    
      I was suprised no one else came up with the magic increment. I had
      perl -e 'print"$_ "for"002".."020"'
      since I didn't remember $,.

      Look Ma! No quotes!

      perl -l40eprint,for'002'..'020'

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

        Nice. 24.. you guys are getting dangerously close. ;-) No quotes, btw? Then what are those four thingies? ;^)

        Makeshifts last the longest.

        That didn't work for me. All it did was print a space. (Both 5.6.0 on AIX and 5.8.0 on Solaris.)

        ------
        We are the carpenters and bricklayers of the Information Age.

        The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Gee... that doesn't work for me. Here's why:

        $ perl -MO=Deparse -l40eprint,for'002'..'020' BEGIN { $/ = "\n"; $\ = " "; } print($_), 'for002' .. 16; -e syntax OK

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by Mr. Muskrat (Abbot) on Aug 14, 2003 at 19:21 UTC
    perl -le 'printf("%03u ",$_)for(2..20)'
              '1234567890123456789012345678'
      Minor improvement: all the parentheses are unnecessary.
      perl -le'printf"%03u ",$_ for 2..20'

      -- Mike

      --
      XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
      -- grantm, perldoc XML::Simpler

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by Aristotle (Chancellor) on Aug 14, 2003 at 19:30 UTC
    22, you probably want to count the -l though, so 23.
    # 1234567890123456789012 perl -le'$#=" %03g";print 2..20'
    Why are we obscuring golf solutions?

    Makeshifts last the longest.

      Nice. Except for that leading space in the output... Still, you get an upvote from me.

      Update: Of course, you can fix the leading space with "$#="%03g " instead. I can't tell if the example output had a space on the end, so we'd have to let that slide, I guess. (BrowserUk's has a space on the end too, afterall.) That just leaves us with the fact that $# is deprecated. It works... for now. :-)

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by dragonchild (Archbishop) on Aug 14, 2003 at 19:31 UTC
    *grins* I don't know how cheating this is, but ...
    echo '"000" .. "020"' | perl -ple '@f=eval;$_="@f"'

    Between the quotes, I'm at 14. Woo-hoo! *laughs*

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Well, if you want to go that route..
      echo '@_ = "002" .. "020"; print "@_\n"' | perl -ne 'eval'
      Four characters! (Reminds me of "how do you write a LISP interpreter in LISP?".)

      Makeshifts last the longest.

(it's thursday here) Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by particle (Vicar) on Aug 14, 2003 at 19:49 UTC

    /me wishes it was friday...

    ~Particle *accelerates*

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by benn (Priest) on Aug 14, 2003 at 19:51 UTC
    My 1st PM golf entry :)
    perl -e 'print"0$_ "for"02".."20"' 123456789012345678901234
    Ben.
Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by jsprat (Curate) on Aug 15, 2003 at 01:12 UTC
    23 (24 with the -l), and doesn't print the trailing space!

    $perl -le'$,=$";print"002".."020"' 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 01 +9 020 $perl -e'print length q/$,=$";print"002".."020"/' 23
    Update: I guess I was beaten to the punch. sauoq's is an update, and it wasn't there when I began writing this post (I searched the page for $" before I started writing my post).
      That's exactly what sauoq already posted a while ago.

      Makeshifts last the longest.

Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by demerphq (Chancellor) on Aug 15, 2003 at 09:58 UTC

    24 chars. But I bet its already posted. (havent checked to keep an open mind.)

    perl -e "$,=' ';print'002'..'020'" perl -e '$,=" ";print"002".."020"'

    I must be missing something though.. Surely this is obvious?


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

    • Update:  
    Yep. It _was_ obvious. I'm so glad. :-) Nice one to those that remembered the value of $" (which didnt occur to me to use for some reason.)


Re: (Golf) Friday Golf: Print Formatted Sequence of Digits
by fletcher_the_dog (Friar) on Aug 15, 2003 at 23:24 UTC
    Total Cheater method:
    First: Create module called "i.pm" that looks like this:
    print join(" ", map { sprintf("%03u", $_) } (2..20)), "\n";exit 0
    Then I can get it to 7 characters:
    perl -Mi

      Er, this waits on STDIN for a program to be piped in.


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
        That is why it says "exit 0" at the end! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://283960]
Approved by TStanley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-07-31 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls