[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: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.

May I add your solution to the web page?


> And one that is slightly shorter:
> 
> 	sub hole {pop=~/./;$'?hole($&+$'):$&}

Hmm, we didn't consider recursion when we wrote the guidelines...  :D


Ronald

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