For some reason I usually don't seem to have time to take part in golfs that I've designed. Almost happened with the badly named (couldn't come up with anything better) Subproduct too, but in the end I decided that I didn't really need to write that seminar report yet...

The problem was simple. Given a string of digits (maximum length 20) and a maximum substring length N (maximum of 9), find the largest product of the digits in a substring of 1..N characters. (For example for the string 0120340 and a N of 5 the correct answer is 3*4=12). The most complex parts about the problem are handling zeros correctly and keeping track of the maximum value encountered (the usual golf idiom of using the length of an array for this doesn't work, since the maximum value of 9**9 would require an array that's too large).

Here's the code (68 characters):


First we get the first command line parameter into $_with shift, and loop over it using s///. The answer will be saved into $\ so that we can use just a print without any arguments to print it. Of course print without arguments will print $_too, so we need to empty $_ somehow. This is the reason for using s/.//, while the shorter s/// would otherwise also suffice.

For each position in the input string we'll first initialize $^ to one. After that we'll loop N times through a loop, where $_ has been initialized to "$`$&"(that is, all characters up to and including the one that's currently being processed by s///). In the loop, we'll chop off digits from the end of the newly constructed $_ and multiply $^ with them. If $^ is larger then the current value of $\, set $\ to "$^\n".

There are a lot of variations on this theme that are equally long. I ended up submitting one of the more obfuscated ones, only to regret it now :-)