Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: TPR(0,1) Golf Tournament Begins!

by Amoe (Friar)
on Mar 01, 2002 at 17:46 UTC ( #148626=note: print w/ replies, xml ) Need Help??


in reply to TPR(0,1) Golf Tournament Begins!

Here's a basic, non-golf implementation as a starter.

#!/usr/bin/perl -w use strict; my $secret = shift; until (length $secret == 1) { print "$secret\n"; $secret = tpr($secret); } print "$secret\n"; sub tpr { my @digits = split //, shift; my $not_so_secret; while (my @pair = splice @digits, 0, 2) { unshift @digits, $pair[1] unless !@digits; my $res = $pair[0] + $pair[1]; until ($res <= 9) { my $cpy = $res; $res = 0; for (split //, $cpy) { $res += $_; } } $not_so_secret .= $res; } return $not_so_secret; }


--
my one true love


Comment on Re: TPR(0,1) Golf Tournament Begins!
Download Code
Re: Re: TPR(0,1) Golf Tournament Begins!
by Amoe (Friar) on Mar 01, 2002 at 17:55 UTC

    Heh...the experts are already way ahead of me, but as a beginner, I thought I'd try anyway. Apparently, 229 chars with very simple whitespace removal:

    my$s=shift;until(length$s<2){print"$s\n";$s=tpr($s)}print"$s\n";sub tpr{my@d=split//,shift;my $s;while(my@p=splice@d,0,2){unshift@d,$p[1] unless!@d;my$r=$p[0]+$p[1];until($r<=9){my$c=$r;$r=0;for(split//,$c){ $r+=$_}}$s.=$r}$s}

    Update: abuse of map brings it to 227. Is whitespace counted? I'd hate to make it a horrible one-line thing...

    my$s=shift;until(length$s<2){print"$s\n";$s=tpr($s)}print"$s\n";sub tpr{my@d=split//,shift;my $s;while(my@p=splice@d,0,2){unshift@d,$p[1] unless!@d;my$r=$p[0]+$p[1];until($r<10){my$c=$r;$r=0;map{$r+=$_}split //,$c}$s.=$r}$s}


    --
    my one true love

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://148626]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-07-05 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls