Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Golf: 3 hole golf game

by etcshadow (Priest)
on Nov 17, 2004 at 06:31 UTC ( #408322=note: print w/replies, xml ) Need Help??


in reply to Golf: 3 hole golf game

human.pl: 103 strokes shuffle.pl: 37 strokes select.pl: 37 strokes ------------------------ total: 177 strokes
Although my human.pl is (sadly) a complete ripoff of Tilly's... I just changed the arbitrary limit to a silly but not arbitrary one. Also, no modules for #2 or #3.

human.pl :::::::::::::: @x=<>;$z=length"@x";%x=map{($x=lc)=~s/\d+/0 x($z-length$&).$&/ge;$_,$x}@x;print sort{$x{$a}cmp$x{$b}}@x :::::::::::::: select.pl :::::::::::::: @_=<>;print splice@_,rand@_,1for 1..2 :::::::::::::: shuffle.pl :::::::::::::: print splice@_,rand@_,1for 1..(@_=<>)

Frankly, I feel that this should count as a solution to #1, but it doesn't, because perl's native arithmetic comparison is just broken on ludicrously long numbers:

:::::::::::::: human_shoulda.pl :::::::::::::: print map{@$_}sort{$z=0;{$z>@$a?0:lc$$a[$z]cmp lc$$b[$z++]||$$a[$z]<=> +$$b[$z++]||redo}}map{[split/([\d\n]+)/]}<>

Oh, well. It's still longer than my cleaned-up version of Tilly's, so I guess it's no real loss.

------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: Golf: 3 hole golf game
by tilly (Archbishop) on Nov 17, 2004 at 16:04 UTC
    And ripping off dragonchild's improvements on my human.pl, plus adding one improvement on your strategy we get to 94 characters:
    @x=<>;print sort{$x{$a}cmp$x{$b}}map{($x{$_}=lc)=~s/\d+/0 x(length("@x")-length$&).$&/ge;$_}@x
    Incidentally nice work on the select and shuffle problems. Marginally less efficient than my solution, but a lot shorter. (Well technically your solution is quadratic, but the quadratic bit has a small constant.)
      Nice.

      Was efficiency a requirement? If it was, then oops. Oh, well, I was optimizing for brevity.

      P.S. I wasn't able to inline the "@x" when I tried it (must be because I'm using a much older perl). I didn't throw in the $z="@x"; just to waste space. I was actually pretty disapointed at having to do so. Nice. I should really upgrade my perl (or at least have a more up-to-date version around).

      ------------ :Wq Not an editor command: Wq
        Brevity was indeed the requirement. I was just trying to make myself feel better. ;-)
Re^2: Golf: 3 hole golf game
by dragonchild (Archbishop) on Nov 17, 2004 at 12:59 UTC
    You can get another stroke out of select.pl by changing your elipsis to a comma. That gets you to 36.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Oh, yeah, duh. :-D

      It should be obvious that my select.pl is just a trivial modification to my shuffle.pl. I obviously didn't spend long enough converting it over. Thanks.

      ------------ :Wq Not an editor command: Wq

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2019-10-14 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?