diff options
| -rwxr-xr-x | challenge-002/ailbhe-tweedie/perl5/ch-02.pl | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl index ac6174c6bd..f701e59a25 100755 --- a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl +++ b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl @@ -6,6 +6,8 @@ # representation, using the characters 0-9 and A-Y. # # Prerequisites: cpan install Scalar::Util::Numeric +# +# Usage: echo {0.999} | ./ch-02.pl use strict; use warnings; @@ -16,39 +18,39 @@ my $BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY"; while (<>) { chomp; - my $input = $_; - next unless isint $input; - my $output = convertToBase35($input); - print "output: $output\n"; + # TODO: find out how to stream input, instead of saving it to an array first + my @input = split; + for my $i (@input) { + next unless isint $i; + my $output = toBase35($i); + printf "%-4s\t==>\t%-4s\n", $i, $output; + } } -sub convertToBase35 { +sub toBase35 { my $input = shift; - my $orig = $input; my @base = split "", $BASE; my @convert; my $baseNum = @base; - print "$baseNum\n"; my $max = 0; while (1) { - last unless $baseNum ** ++$max < $input; + last unless $baseNum ** ++$max <= $input; } while ($max > 0) { my $exp = $max - 1; - print "$baseNum to the $exp is ", $baseNum ** $exp, " which is smaller than $input\n"; + #print "$baseNum to the $exp is ", $baseNum ** $exp, " which is smaller than $input\n"; my $pow = $baseNum ** $exp; my $place = 0; while ($pow <= $input) { $input -= $pow; $place++; - print "$input; $place\n"; + #print "input $input; place $place\n"; } push @convert, $place; $max--; } - dd @convert; - foreach my $b (@base) { - print "-$b-\n"; - } + #print "@convert: "; + #dd @convert; + #print "\n"; my $output = join "", @base[@convert]; } |
