Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

TIMTOWTDI/golf - counting backwards

by Juerd (Abbot)
on Jan 28, 2002 at 19:54 UTC ( [id://142074]=perlmeditation: print w/replies, xml ) Need Help??

Another round of TIMTOWTDI/golf :)

Printing 1..10 was simple, but counting backwards from 10 to 1 is a bit harder, because the .. list operator can't do that. It's even harder when you can't use reverse *evil grin*
    The rules:
  • No \w characters
Just kidding :)
    The rules:
  • No numbers (no \d characters)
  • No use of reverse
  • No modules
  • Pure Perl
As usual, extra points for the original and the golfed.
(And an extra bonus if you're able to do it without \w characters ;))

My try: perl -le'$b=ord "\cJ"; $a++ == ord "\cJ" && die, print $b-- while 1'
perl -le'$b=ord "\cJ"; $a++ == ord "\cJ" && die, print $b-- while "foo"'

Update - chipmunk correctly pointed out that I didn't follow my own rules... Oops! :)

Replies are listed 'Best First'.
Re: TIMTOWTDI/golf - counting backwards
by japhy (Canon) on Jan 28, 2002 at 20:19 UTC
    perl -le'$_=__________;_ while s/_/print length;""/e' And even better: perl -le'__________=~/^.+(?{print@+})^/'

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: TIMTOWTDI/golf - counting backwards
by Masem (Monsignor) on Jan 28, 2002 at 20:16 UTC
    Probably want to get this variation out of the way... :-)
    sort { $b <=> $a } ( (length '.' ) .. (length '..........') );
    (Specifically the sort context)

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: TIMTOWTDI/golf - counting backwards
by gmax (Abbot) on Jan 28, 2002 at 20:20 UTC
    # 1 2 3 #23456789012345678901234567890123456 $a=ord(L)-ord(A);print$a.$/while$a-- # wrong one $a++for(a..j);print$a--.$/for(a..j)
    update 1 sorry the first one prints from 10 to 0. Fixed the second one.

    update 2 These ones are too long, but intriguing.
    perl -e '$b++;$a=hex--$b.xA;print$a--.$/for(a..j)' perl -e '$a=++$b.$[;print$a--.$/for(a..j)'
    update 3
    This one seems to be short enough (30)
    # 1 2 3 #23456789012345678901234567890 $a=hex(A);print$a--.$/for+a..j $a=hex+A;print$a--.$/for+a..j # (29) with a warning
    _ _ _ _ (_|| | |(_|>< _|
Re: TIMTOWTDI/golf - counting backwards
by jmcnamara (Monsignor) on Jan 28, 2002 at 20:49 UTC

    45 chars. It uses revers but not reverse.
    #23456789_123456789_123456789_123456789_12345 perl -le'$=/=split//,revers;print$=--while$='

    --
    John.

      Nice one, jmcnamara. It took a while before I figured out how it works, mostly because I didn't know what split does in scalar context, but the /= might confuse quite a few people too :)

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: TIMTOWTDI/golf - counting backwards
by chipmunk (Parson) on Jan 28, 2002 at 20:48 UTC
    35 characters for the whole command line: perl -le'$a=ord$/;print$a--while$a' Note: will not work on a Mac. :)
      Note: will not work on a Mac. :)
      ...works fine on my Mac, chipmunk.
      What's wrong with yours? :-)
      [localhost:~/Music] bbking% uname -a Darwin localhost 1.4 Darwin Kernel Version 1.4: Sun Sep 9 15:39:59 PD +T 2001; root:xnu/xnu-201.obj~1/RELEASE_PPC Power Macintosh powerpc [localhost:~/Music] bbking% perl -le'$a=ord$/;print$a--while$a' 10 9 8 7 6 5 4 3 2 1
Re (tilly) 1: TIMTOWTDI/golf - counting backwards
by tilly (Archbishop) on Jan 28, 2002 at 20:40 UTC
    I am unclear. Are we talking a command line program or a function?
    #23456789_123456789_123456789_12345678 perl -le'$_=@{[a..k]};print while--$_' sub f { #23456789_123456789_123456789_12 @_=a..j;(print@_.$/),pop while@_ }
    UPDATE
    jmcnamara pointed out that I was miscounting. Fixed.
Re: TIMTOWTDI/golf - counting backwards
by broquaint (Abbot) on Jan 28, 2002 at 20:23 UTC
    I've got
    sub f { ($x,$y)=@_;print$x--while$x>=$y }
    So that's a hole in 32, although it only works for numbers, so it's not a literal implementation of .. reversed.

    broquaint

    Update: I may have misunderstood the original question so here's my second (possibly more accurate?) shot

    perl -le '$_=ord$/;print$_--while$_'
    Which comes to 26, and satisfies chipmunk's need for ouput :o)

    Update 2: I've just noticed my solution is exactly the same aforementioned monk, except I've used $_ instead of $a as my var name (I had hoped to exploit $_ in some way ...). Spooky.

      I tried running your solution, but it didn't produce any output...
Re: TIMTOWTDI/golf - counting backwards
by jmcnamara (Monsignor) on Jan 28, 2002 at 21:59 UTC

    33 chars. Is that the winner?:
    #23456789_123456789_123456789_123 perl -le'print$=+$=-ord for+n..w'

    --
    John.

Re: TIMTOWTDI/golf - counting backwards
by trs80 (Priest) on Jan 28, 2002 at 21:25 UTC
    Here are my four variations:
    $c=ord"\cJ";while($c){print$c--.$/}; $c=ord"\cJ";print$c--.$/for(a..j); for($c=ord"\cJ";$c;$c--){print$c.$/}; $c=ord"\cJ";map{print$c--.$/}(a..j);
    UPDATE: I had blindly followed the example and used the \cJ when in fact it was just a \n. Also gmax pointed out that my first example could be shorten by moving the while statement. So here is a revised entry:
    $c=ord"\n";print$c--.$/while$c;
Re: TIMTOWTDI/golf - counting backwards
by Aristotle (Chancellor) on Jan 28, 2002 at 23:46 UTC
    I started out trying to golf but ended up with what I think is a remarkable obfu instead.. :-)
    perl -pe'BEGIN{$i=$j=ord$/}$_=($i--||exit).chr$j' </dev/urandom
    ____________
    Makeshifts last the longest.
Re: TIMTOWTDI/golf - counting backwards
by Juerd (Abbot) on Jan 28, 2002 at 21:48 UTC
    Here's my first _serious_ try ;) [depends on ord($/) to be 10]

    including command line:
    # 1 2 3 #2345678901234567890123456789012345678 perl -le'print-!$|*$a+++ord$/for a..j'
    Generic Perl (needs $/):
    # 1 2 3 #234567890123456789012345678901 print-!$|*$a+++ord$/,$/for a..j

    So far, I beat japhy by 2 :)
    Superior to me is chipmunk, who didn't only spot my mistake in the root of this thread, but also has a version that's 3 characters shorter than mine.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      Hrm, and I didn't get any notice for having another solution that is as short as your function, and having one that is 1 better on the command line?

      Guess I will have to improve then.

      #23456789_123456789_123456789_12345 perl -le'$x=@_=a..j;print$x--for@_' sub f { #23456789_123456789_12345678 $x=@_=a..j;print$x--.$/for@_ }
      UPDATE
      The answer to the question is "tradition". :-)

      UPDATE 2
      jmcnamara pointed out that I was miscounting. Fixed.

        The sub more or less made me assume it takes parameters. Why do you write it as a sub when it works perfectly on its own?

        2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: TIMTOWTDI/golf - counting backwards
by jynx (Priest) on Jan 29, 2002 at 00:03 UTC

    Hmm,

    are we still not allowed numeric operators (like last time?) Here's my shortest, followed by a rehashing of what i did last time, which is essentially a variation on the sort{$b<=>$a} trick. The lack of \w characters would be more interesting, i think i'll try one if i get some free time today...

    #1 (at 35 chars) map{print@_.$";pop@_}@_=($")x ord$/ #2 $$_=~/((((((((((.))))))))))/for$|..ord$/;print map $_.$",sort{chr$b cmp chr$a}grep/\d/&$||$_,keys%::
    enjoy,
    jynx

    Update: i have a version with no \w characters which can be found below


      In order to free up my scratchpad, here's my entry for the no \w characters portion of this contest. There are numeric operators, but no underscores (which was a trick unto itself). The invocation of the script is not counted towards the "no \w character limit", or at least i didn't count it...

      Well, ok, the first version uses 4 letters ('e', 'v', 'a', and 'l') and the second version reuires a special invocation. And no, neither of them are remotely strict or warning compliant. But hey, it's the best i could do. i'd be interested in seeing other's attempts (should they choose to go insane and try this as well :)

      enjoy,
      jynx

      # 1 # requires no special invocation, uses 'e','v','a','l' %!=('!'=>$|+@{[$"]},'@'=>$|+@{[$",$"]});($;{'!'})=$::{'"'}=~/\*.(.)/ ;$;{'^'}=$;{'#'}=$;{'!'};$;{'~'}=++$;{'^'};$;{'='}=++$;{'^'};$;{'(' }=++$;{'^'};$;{'!'}=($;{'*'}=++$;{'^'}).$;{'!'};$;{'*'}++;$;{'*'}++ ;$;{'='}.=++$;{'*'};($;{'@'})=$::{'"'}=~/\*..(.)/;$;{'&'}=$;{'@'};$; {'&'}++;$;{'&'}++;$;{'!'}.=++$;{'&'};($;{'*'})=$::{'@'}=~/\*(.)/;$; {'#'}=$;{'*'}.$;{'#'};($;{'&'})=$::{'$'}=~/\*...(.)/;$;{'@'}.=$;{'&' };$;{'%'}=$;{'$'}=++$;{'&'};$;{'('}=$;{'%'}.$;{'('};$;{'*'}=++$;{'&' };$;{'@'}=$;{'*'}.$;{'@'};$;{'#'}.=$;{'&'};$;{'$'}=$;{'*'}.$;{'$'}. $;{'&'};$;{'*'}++;@;=$;{'('}=~/(.)(.)/;$;{'('}=$;[$|].++$;{'*'}.$;[ $!{'!'}];$;{'='}.=$;{'*'};@;=$;{'@'}=~/(.)(.)(.)/;$;{'@'}=$;[$|].$; {'*'}.$;[$!{'!'}].$;[$!{'@'}];$;{'~'}=++$;{'*'}.$;{'~'};$;{'@'}.=++ $;{'*'};@;=$;{'~'}=~/(.)(.)/;$;{'~'}=$;[$|].++$;{'*'}.$;[$!{'!'}];@; =$;{'!'}=~/(.)(.)(.)/;$;{'!'}=$;[$|].++$;{'*'}.$;[$!{'!'}].$;[$!{'@' }];($;{'*'})=$::{'"'}=~/\*(.)/; eval $;{'~'}.$".$;{'^'}.'{'.$;{'!'}.$".$; {'$'}.'}';$;{'^'}->($;{'~'}.$".$;{'%'}.'{'.$;{'('}.$".$;{'$'}.'}');$; {')'}=$;{'^'}->($;{'='}.'($=+'.$;{'^'}->($;{'('}.'"#"').')');$;{'^'} ->($;{'~'}.$".$;{'&'}.'{'.$;{'@'}.'@'.$;{')'}.'}');$;{'^'}->($;{'~'} .$".$;{'*'}.'{'.$;{'#'}.'{'.$;{'&'}.'(@'.$;{')'}.'.$");'.$;{'$'}.'@' .$;{')'}.'}@'.$;{')'}.'}');$;{'*'}->($",$",$",$",$",$",$",$",$",$") # 2 # call using: perl -e 'sub e{eval"@_"}do"./foo"' # where 'foo' is the filename where the code is stored %!=('!'=>$|+@{[$"]},'@'=>$|+@{[$",$"]});($;{'!'})=$::{'"'}=~/\*.(.)/ ;$;{'^'}=$;{'#'}=$;{'!'};$;{'~'}=++$;{'^'};$;{'='}=++$;{'^'};$;{'(' }=++$;{'^'};$;{'!'}=($;{'*'}=++$;{'^'}).$;{'!'};$;{'*'}++;$;{'*'}++ ;$;{'='}.=++$;{'*'};($;{'@'})=$::{'"'}=~/\*..(.)/;$;{'&'}=$;{'@'};$; {'&'}++;$;{'&'}++;$;{'!'}.=++$;{'&'};($;{'*'})=$::{'@'}=~/\*(.)/;$; {'#'}=$;{'*'}.$;{'#'};($;{'&'})=$::{'$'}=~/\*...(.)/;$;{'@'}.=$;{'&' };$;{'%'}=$;{'$'}=++$;{'&'};$;{'('}=$;{'%'}.$;{'('};$;{'*'}=++$;{'&' };$;{'@'}=$;{'*'}.$;{'@'};$;{'#'}.=$;{'&'};$;{'$'}=$;{'*'}.$;{'$'}. $;{'&'};$;{'*'}++;@;=$;{'('}=~/(.)(.)/;$;{'('}=$;[$|].++$;{'*'}.$;[ $!{'!'}];$;{'='}.=$;{'*'};@;=$;{'@'}=~/(.)(.)(.)/;$;{'@'}=$;[$|].$; {'*'}.$;[$!{'!'}].$;[$!{'@'}];$;{'~'}=++$;{'*'}.$;{'~'};$;{'@'}.=++ $;{'*'};@;=$;{'~'}=~/(.)(.)/;$;{'~'}=$;[$|].++$;{'*'}.$;[$!{'!'}];@; =$;{'!'}=~/(.)(.)(.)/;$;{'!'}=$;[$|].++$;{'*'}.$;[$!{'!'}].$;[$!{'@' }];($;{'*'})=$::{'"'}=~/\*(.)/;$;{'~'}.$".$;{'^'}.'{'.$;{'!'}.$".$; {'$'}.'}';$;{'^'}->($;{'~'}.$".$;{'%'}.'{'.$;{'('}.$".$;{'$'}.'}');$; {')'}=$;{'^'}->($;{'='}.'($=+'.$;{'^'}->($;{'('}.'"#"').')');$;{'^'} ->($;{'~'}.$".$;{'&'}.'{'.$;{'@'}.'@'.$;{')'}.'}');$;{'^'}->($;{'~'} .$".$;{'*'}.'{'.$;{'#'}.'{'.$;{'&'}.'(@'.$;{')'}.'.$");'.$;{'$'}.'@' .$;{')'}.'}@'.$;{')'}.'}');$;{'*'}->($",$",$",$",$",$",$",$",$",$")
Re: TIMTOWTDI/golf - counting backwards
by belg4mit (Prior) on Jan 29, 2002 at 05:12 UTC
    Here's my attempt (without peeking):
    perl -e '$i=print map ord,qw"m W A +";print print++$i'
    That's 44 characters

    --
    perl -pe "s/\b;([st])/'\1/mg"

Re: TIMTOWTDI/golf - counting backwards
by insensate (Hermit) on Jan 29, 2002 at 03:19 UTC
    Here's my go:
    perl -le'@j=(A..K);print$#j--while$#j'
Re: TIMTOWTDI/golf - counting backwards
by BrentDax (Hermit) on Jan 29, 2002 at 03:47 UTC
    Don't laugh--it's my first golf. 55 chars, appears to follow the rules.

    $,=$";print sort{$b<=>$a}(ord(b)-ord a)..(ord(k)-ord a)

    UPDATE: This occurred to me about five minutes after that one. 45 chars.

    $,=$";print sort{$b<=>$a}map{ord()-ord a}b..k

    =cut
    --Brent Dax
    There is no sig.

      A quick golf tip....
      sort{$b<=>$a}@arr
      Can be golfed to:
      sort{$b-$a}@arr

      -Blake

Re: TIMTOWTDI/golf - counting backwards
by particle (Vicar) on Jan 29, 2002 at 05:26 UTC
    okay, 43 characters. not a winner. but it works under strict and warnings, and i just love the way it looks.

    $_="$[thisisperl";print length while chop;

    ~Particle

Re: TIMTOWTDI/golf - counting backwards
by petral (Curate) on Jan 29, 2002 at 06:01 UTC
    Not as clever as jmcnamara or belg4mit or as short as chipmunk. Oh well.
    perl -le'$^=~s#.#$_=$'\'';print s/.|$//g#ge'

    or 5.6:
    perl -le'$^=~s/.$/!print@+/e while$^'

    update: but this works with 5.005:
    perl -le'$_=$^;s||s/.$/!print length/e|ge'

      p
Re: TIMTOWTDI/golf - counting backwards
by redsquirrel (Hermit) on Jan 31, 2002 at 17:14 UTC
    This was a fun one I whipped up real quick, it's definitely not a winner (43), but fun to write:
    perl -e'print index xwvutsrqpon,$_ for n..w'
    --Dave
Re: TIMTOWTDI/golf - counting backwards
by belden (Friar) on Jan 31, 2002 at 08:45 UTC
    Using hex seems to be an abuse of the rules, but what the heck: gotta start golfing sometime. At 30 characters:
    #23456789_123456789_123456789_ $_=hex a;print$_--,"\n"while$_
    Update: juerd pointed out that I can't count. Fixed. (I was claiming 34 characters.)
    blakem's hint brings me down to 28 characters
    #23456789_123456789_12345678 $_=hex a;print$_--,$/while$_
    blyman
    setenv EXINIT 'set noai ts=2'
      #2346789_23456789_23456789_234
      Skipped school again, huh? :)

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      In golf, "\n" is spelled $/

      -Blake

Re: TIMTOWTDI/golf - counting backwards
by demerphq (Chancellor) on Jan 31, 2002 at 16:51 UTC
    I suspect this may have been already done, but without looking at any other replies I get 38 chars.
    UPDATE pulled 3 chars off by using $/ instead of "\cJ"... Yowzer! chimpmunk++!
    #12345678901234567890123456789012345678 unshift@a,++$;while$;<ord"\cJ";print@a unshift@a,++$;while$;<ord$/;print@a
    Now to see the other solutions... :-)

    Yves / DeMerphq
    --
    When to use Prototypes?

Re: TIMTOWTDI/golf - counting backwards
by stefp (Vicar) on Feb 02, 2002 at 01:34 UTC
    map{print+substr-"$_$/",1}-ord"\cJ"..-ord"\cA"

    I need the substr() to get read of spurious pluses

    -- stefp -- check out TeXmacs wiki

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-19 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found