Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: (Golf) Let's go bowling

by dga (Hermit)
on Aug 11, 2001 at 00:23 UTC ( #104036=note: print w/replies, xml ) Need Help??


in reply to Re: (Golf) Let's go bowling
in thread (Golf) Let's go bowling

Per my update above this one is less than 1/3 the other and still works if you use use strict; (and warnings)

{ my(@g)=@_; my(@c)=@g; my($t, $s, $m, $p, $f, $h); foreach (@c) { s/[Xx]/10/; s/\//10-$p/e; $p=$_; } while(@g) { $s=shift(@c); $_=shift(@g); /X/ && ($t += $s + $c[0] + $c[1], $f++, $h=0, $f>9?last:next) +; /\// && ( $t += $s + $c[0], $f++, $h=0, $f>9?last:next); $h=$h?($f++,0):1 $t+=$s; $h++; } $t; }

Dropping the my lines will of course stop strict from letting it run and will erase the callers copy of all the arguments to the function. But it is more than 10% smaller...

Update: Failed case 6 of virtualsue's test suite now works correctly.

Replies are listed 'Best First'.
Re: Re: Re: (Golf) Let's go bowling
by dragonchild (Archbishop) on Aug 11, 2001 at 00:54 UTC
    Rewriting your code in Golf-ese, just to give you an example, could yield (without really taking into account all the shortcuts) in 166 characters ...
    map{s/X/10/;s#/#10-$p#e;$p=$_}@c=@_;$t=$f=$h=0;while(@_){$s=shift@c;$_ +=shift;/[X\/]/&&($t+=$s+$c[0]+(/X/?$c[1]:0),$h=0,++$f>9?last:next);$f +++,$h=0 if$h;$t+=$s;$h++}$t
    Golf code isn't meant to be pretty or even readable. It's meant to be compact. :)

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      Ok last draft, including dragonchild's compressions and optimizations and correcting the case 6 problem which actually saves 3 more bytes.

      map{s/X/10/;s#/#10-$p#e;$p=$_}@c=@_;$t=$f=$h=0;while(@_){$s=shift@c;$_ +=shift;/[X\/]/&&($t+=$s+$c[0]+(/X/?$c[1]:0),$h=0,++$f>9?last:next);$h +=$h?($f++,0):1;$t+=$s}$t

      163 bytes which is basically 1/2 of the previous length.

      Thanks again dragonchild.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://104036]
help
Chatterbox?
[Cosmic37]: greetings earthlings please can someone improve my pathetic perl knowledge
[Cosmic37]: I have 2 files each with datetime and other data in unknown order and I want to find rows from both files matching by datetime and output them combined/ concatenated
[Corion]: Sure
[Corion]: Do you have any specific interests or general Perl knowledge?

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-06-29 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (672 votes). Check out past polls.