[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Fun With Golf



On Wed, Oct 18, 2000 at 12:48:21PM -0400, Ronald J Kimball wrote:
> On Wed, Oct 18, 2000 at 12:37:45PM -0400, yanick1@sympatico.ca wrote:
> > perlgolf hole - Digital Root
> > 
> > Write a subroutine to compute the digital root of any whole number argument.
> > 
> > Winner: sub hole {$_=pop;1while s/(\d)(\d)/$1+$2/ge;$_}
> > 
> > 
> > Can't this function can be triviality optimized of three whole characters
> > as
> > 
> > 	sub hole {$_=pop;1while s/(.)(.)/$1+$2/e;$_}
> > 
> > ?
> > 
> > (we while the s///, so we can away with the g, and the statement 
> > says the argument is a number, do we can switch \d by .)
> 
> You're right, you just shaved three characters off of our shortest
> solution!  Nice work.

	I'm right? Really? Woohoo! :)

> May I add your solution to the web page?

	I would be honored.

> > And one that is slightly shorter:
> > 
> > 	sub hole {pop=~/./;$'?hole($&+$'):$&}
> 
> Hmm, we didn't consider recursion when we wrote the guidelines...  :D

	Understandable. After all, most people would prefer to chew
their own arms off rather than tackle recursion. :) But I don't 
see why recursion should be forbidden. After all, it's not using
any external function, and it does return what we want. Okay, it's 
evil, but it satisfy one of the Great Virtues of Perl: laziness. 
"Don't do it if you can ask yourself to do it", if you wish. :)

	This being said, three other characters can be saved if 
I could have named the functin 'h'. ;)

Joy,
Yanick

-- 
$,=' '; $\="\n"; @_= qw/a a a a/; @s= qw/17049 482466755 285075 95568295/;
while( (sort @s)[-1] ){ map $s[$_]-->0&&++$_[$_], (0..$#_); print @_; } 
                    ### May take some time. :) ###

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe