Front page | perl.fwp | Postings from March 2002

## Re: TPR1 post-mortem

From:
fun_with_perl
Date:
March 8, 2002 13:15
Subject:
Re: TPR1 post-mortem
Message ID:
a6b9i8\$ejj\$1@post.home.lunix
```In article <02030808424001.14566@jason.journalistic.com>,
Jason Purdy <jason@journalistic.com> writes:
> Someone's gonna explain these, right?  Man, I love learning this kinda stuff.
>  Last "hole", I learned about pop vs. shift (saving two strokes) and after
> seeing this e-mail and playing around with Perl, I see that "-l" will take
> care of the newline stuff for me (so I don't have to add "\$_\n" to my print
> statements [6 strokes there]).

Definitely have a look at the archives of some old golf. Also look
at http://a108.bauhuette.haw-hamburg.de/golf/challenge.html
>
> Is that #*1.11%10 a number theory to get to the same number?  How did someone
> recognize that pattern? (my advanced calculus/comb math being a lil' rusty)

In my case it was because i was thinking about how you do multiplication

ab
ab
ab
----
ASsb

where s is the last digit of the sum of a and b, and S is the same, but
with the carry from s added in. And THAT is exactly what we were supposed
to calculate. The sift add pattern you get from 111. So we need the
third-last digit from \$num*111, which you can get by dividing by 100
and modulus 10, so you get:

\$num*1.11%10

Also, if a+b is small enough as not to give carry (which happens if the
number is 18 or lower), you can use:

\$num*1.1%10

The hex() trick i found when enumerating all functions in perlfunc and
thinking about which one changes behaviour at 10. Then you see that
numbers < 10 get through undamaged, and 10 becomes 16 (should be 1) and
they count further again. so add modulus 15 to compensate.

It's only useful for the case where you have two digits from different places.
you need either + or . to combine them, and that has rather low precedence,
so you tend to need the parenthesis anyways, so in a sense the cost is only
6 strokes.

PS: the entries in the scoreboard are not ordered by tie score.
It's the entry with &&do\$0 that won (better tie-score)

Also a note about looping: in golf only these loops tend to happen
if you need a conditional loop (unlike a fixed loop where s///eg, map and
modifier for are also useful), whose lengths are sometimes a bit hard to
estimate since they depend on how much you can cuddle the thing before
and after:

- modifier until/while	length 5 upto 7 if you can't cuddle
- for(1;2;3){4}	length 7 but only if you can fill all slots.
Therefore almost never the best loop (you can
cuddle up some postfix stuff though)
- &&do\$0		length 6, can be 5 if you can can cause the
called thing to die. Can be 5 or 4 if you can
somehow write the expression so \$0 becomes a
result (e.g. do\${some_expression} or do
\$0|some_expression)

You can basically ignore things like redo (one entry used ..redo for.. in a
way that works, but i rather think that's a perl bug), goto etc. They all
end up longer.

Notice that in this case the number of iterations was constrained by the
input length, so s///eg, map and for based loops are also possible.

Can anyone find counterexmples for other loopingstyles that can be shorter ?

```