Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Golf Soccer Stats

by SparkeyG (Curate)
on May 23, 2002 at 17:07 UTC ( [id://168831]=perlmeditation: print w/replies, xml ) Need Help??

Time to flex those Golf skills.

Soccer leagues are often listed by points earned and games played. A team earns 3 points for a win and 1 point for a tie. Zero points are earned for a loss.
The challenge is to create a function that gets two scalars, gamesPlayed and pointsEarned and returns three scalars, winTotal, lossTotal, and tieTotal.
For this golf, ignore the possibility that the team may have more than 2 ties.

A non-golfed example is presented to give you somewhere to start.

sub record { my ($gp, $pts) = @_; my $win = 3; my $tie = 1; my ($winTot, $lossTot, $tieTot); $winTot = int ($pts / $win); $tieTot = $pts - ( $winTot * $win ); $lossTot = $gp - ( $winTot + $tieTot); return ($winTot, $lossTot, $tieTot); }

Replies are listed 'Best First'.
Re: Golf Soccer Stats
by boo_radley (Parson) on May 23, 2002 at 17:58 UTC
    52 inside the sub:
    print join "\n",&ss (10,19); sub ss {($p,$o)=@_;$t=$o%3;$w=($o-$t)/3;$l=$p-$t-$w;$w,$l,$t}

      For some reason,

      Many people believe that the shortest way to get arguments is either all at once or not at all. Usually i have found this not to be the case. For instance, coupling your statements with your argument pulling i get 46:

      sub ss{ #23456789_123456789_123456789_123456789_123456789_ $l=-($t=$_[1]%3)-($w=(-$t+pop)/3)+pop;$w,$l,$t }
      And if i couple the assignments with the return statement, i can shave off another 8:
      sub ss2{ #23456789_123456789_123456789_123456789_123456789_ $w=(-($t=$_[1]%3)+pop)/3,-$t-$w+pop,$t }
      They seem to test out correctly, but i didn't test all that extensively (engineer's test: if it works 3 times for 3 different input it must work for everything :)

      jynx

      update: it strikes me that i am using a personal definition that i should elaborate on slightly. i think of "pulling arguments" as using pop, shift, or @_ to take an argument out of @_ and assign it to another variable. Not doing so is using $_[$x] directly throughout.

Re: Golf Soccer Stats
by vladb (Vicar) on May 23, 2002 at 17:20 UTC
    Nice challenge..

    Here's my shameful attempt:
    sub record { ($g,$p)=@_; $w=3;$t=1; $wT=int($p/$w); $tT=$p-($wT*$w); $lT=$g-($wT+$tT); ($wT,$lT,$tT); }
    I didn't do much, just obfuscated it a bit here and there ;).

    UPDATE: More Golf:
    sub record { ($g,$p)=@_;$w=3;$t=1; $"=int($p/$w);$tT=$p-($" *$w);$;=$g-($"+$tT); ($",$;,$tT); }


    UPDATE 1: Some more golf :)
    sub record { ($g,$p)=@_; @_=($"=int($p/(@_+1)),$/=$p-$"*(@_+1),$g-$"-$/); (shift,pop,pop) }


    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
Re: Golf Soccer Stats
by pepik_knize (Scribe) on May 23, 2002 at 17:58 UTC
    Here's my try:

    sub record { $t=$_[1]%3;$w=int$_[1]/3;$l=$_[0]-$w-$t;$w,$l,$t # 1 2 3 4 #23456789012345678901234567890123456789012345678 }

    Pepik

    When pride and presumption walk before, shame and loss follow very closely. -- Louis XI.

    Update I had the arguments reversed, and got rid of parentheses around $w,$l,$t.

Re: Golf Soccer Stats
by andreychek (Parson) on May 23, 2002 at 23:33 UTC
    Here's another stab (with help from quadzero):

    Chars: 37
    sub stats { $p=pop;$w=int$p/3,$t=$p%3,$_[0]-$w-$t }
    -Eric

    --
    Lucy: "What happens if you practice the piano for 20 years and then end up not being rich and famous?"
    Schroeder: "The joy is in the playing."
      Close. But you return ($wins, $ties, $losses). The Game is ($wins, $losses, $ties). I only know this because I fell into the same trap, and switching it arround ain't easy.

      update: um ... ain't easy ... uh ... err ... unless your jynx! Yeah, that's the ticket! That's what I ment to say.

      Wow! I am continualy enlightened.

        Not looking to start a war cuz i'm as new to perl as they come but still... this particular golf challenge doesn't *explicitly* say that the order of the results returned is significant. I agree that it does list them in the order w/l/t, but it doesn't say that they *must* be returned in this order.

        As in logic puzzles, i think it makes good sense not to assume anything at all. For example, if the goal was to return a string with the characters "a", "b" and "c" both surrounded with, and separated by, a tilde character ("~"), would you assume that the "correct" results looked like this: "~a~b~c~", or would "~b~a~c~" also qualify?

        In my opinion (and I do mean *just* my opinion :-) both of the above examples qualify as legitimate because the challenge specified the content without specifying the order of the letters. Had it said something along the lines of "return the characters 'a', 'b' and 'c' (in that order) both surrounded by....", then I would certainly agree that the order was an explicit and significant aspect of the solution.

        Anyway, as you've already noticed, if the original soccer score order *was* actually significant, jynx (sp?) took care of that for us.

        Again, not looking for a fight here, just sharing my point of view.

        Cheers! 8-D
        quadzero (at) users (dot) sourceforge (dot) net


      a little rearranging gives 38:
      sub stats { #23456789_123456789_123456789_123456789_ $p=pop;$w=int$p/3,-$w-($t=$p%3)+pop,$t }
      and removing the $p assignment gets it back to 37:
      sub stats2 { #23456789_123456789_123456789_123456789_ $w=int$_[1]/3,-$w-($t=pop()%3)+pop,$t }
      jynx
Re: Golf Soccer Stats
by particle (Vicar) on May 23, 2002 at 19:02 UTC
    49.

    sub r{ # 1 2 3 4 #234567890123456789012345678901234567890123456789 $w=($_[1]-($t=$_[1]%3))/3;$l=$_[0]-$w-$t;$w,$l,$t }

    ~Particle *accelerates*

Re: Golf Soccer Stats
by kal (Hermit) on May 23, 2002 at 19:37 UTC

    47?

    sub record { # 1 2 3 4 #2345678901234567890123456789012345678901234567 ($a,$b)=@_;$d=$b%3;$c=($b-$d)/3;$c,$a-$c-$d,$d; }

    I only tested briefly, I might have missed something..

Re: Golf Soccer Stats
by Aristotle (Chancellor) on May 24, 2002 at 04:56 UTC
    40.
    sub record { # 1234567890123456789012345678901234567890 $w=($_[1]-($t=$_[1]%3))/3,$_[0]-$w-$t,$t }
    ____________
    Makeshifts last the longest.
      Oh come on folks. Every time I make a serious attempt at golfing, everyone disappears. :-(

      :-)
      ____________

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-18 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found