go ahead... be a heretic PerlMonks

### Golf Soccer Stats

by SparkeyG (Curate)
 on May 23, 2002 at 17:07 UTC 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} [download]```

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.

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://168831]
Approved by Albannach
help
Chatterbox?
 [marinersk]: sub newtest{my \$expected_result = &target('foo'); my \$actual_result = &target('foo'); if (\$actual_result eq \$expected_result) { &tdd_success(); } else { &tdd_fail(); } } # Test works after three years! [choroba]: or nobody bothered... [choroba]: The problem was bigger, as the test tried to call a method that didn't exist anymore [marinersk]: :: ducking :: [choroba]: because, someone renamed the method, but didn't notice it was used in the test, as the test was skipped [marinersk]: Well, if the method doesn't exist, it would be hard to pass the test. [choroba]: later, someone removed the new method, as all its usage places were safe, but didn't notice the test still used the old name [choroba]: fortunately, it wasn't that hard to replace the method and fix a few remaining failures due to the changes we did to the codebase over the years [marinersk]: choroba Sounds like a process improvement opportunity; tests may not all need to be run, but they should all be compiled with perl -c before check-in/promotion happens. [choroba]: so, now I have the test, so I can start making changes in the code. Back to the original ticket, yay!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2017-05-25 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (187 votes). Check out past polls.