aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAilbhe Tweedie <atweedie@protonmail.ch>2019-04-02 14:01:44 +0200
committerAilbhe Tweedie <atweedie@protonmail.ch>2019-04-03 01:08:20 +0200
commit176896c928aa3ad1cd7ad98c96f9a010394e1949 (patch)
tree2bc6fe18cfa84fae6b81ab7574fc81bbd00cb744
parent296ac1fd5918ee411beefb03dade953ed28526fe (diff)
downloadperlweeklychallenge-club-176896c928aa3ad1cd7ad98c96f9a010394e1949.tar.gz
perlweeklychallenge-club-176896c928aa3ad1cd7ad98c96f9a010394e1949.tar.bz2
perlweeklychallenge-club-176896c928aa3ad1cd7ad98c96f9a010394e1949.zip
002-p5-02: write fromBase35()
-rwxr-xr-xchallenge-002/ailbhe-tweedie/perl5/ch-02.pl20
1 files changed, 20 insertions, 0 deletions
diff --git a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
index 2624bd7aba..83c63a5eb2 100755
--- a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
+++ b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
@@ -56,3 +56,23 @@ sub toBase35 {
#print "\n";
my $output = join "", @base[@convert];
}
+
+sub fromBase35 {
+ my $input = shift; # APX
+
+ # create a hash converting a base35 alphanumeric character to a base10 value
+ my %hash;
+ my @base = split "", $BASE;
+ for my $i (0..@base-1) {
+ $hash{$base[$i]} = $i;
+ }
+
+ my @based = split "", $input; # (A, P, X)
+ my $max = @based - 1;
+ my $output;
+ while ($max >= 0) {
+ $output += ( @base ** $max ) * $hash{@based[@based - $max - 1]};
+ $max--;
+ }
+ return $output;
+}