aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <adam.russell@optum.com>2019-04-04 13:04:25 -0400
committerAdam Russell <adam.russell@optum.com>2019-04-04 13:04:25 -0400
commit8a77fac92f7e1cfe7e369aedac9f07051417bd88 (patch)
tree8597bbabbb6ddfc724c950214189ce9355ac3bbb
parentf99a6c0cec978e89f58147a8158d16d640d2060f (diff)
downloadperlweeklychallenge-club-8a77fac92f7e1cfe7e369aedac9f07051417bd88.tar.gz
perlweeklychallenge-club-8a77fac92f7e1cfe7e369aedac9f07051417bd88.tar.bz2
perlweeklychallenge-club-8a77fac92f7e1cfe7e369aedac9f07051417bd88.zip
Added solution to challenge-002.
-rw-r--r--challenge-002/adam-russell/README1
-rw-r--r--challenge-002/adam-russell/blog.txt1
-rw-r--r--challenge-002/adam-russell/perl5/ch-1.pl7
-rw-r--r--challenge-002/adam-russell/perl5/ch-2.pl68
4 files changed, 77 insertions, 0 deletions
diff --git a/challenge-002/adam-russell/README b/challenge-002/adam-russell/README
new file mode 100644
index 0000000000..9420c9a781
--- /dev/null
+++ b/challenge-002/adam-russell/README
@@ -0,0 +1 @@
+Solution by Adam Russell
diff --git a/challenge-002/adam-russell/blog.txt b/challenge-002/adam-russell/blog.txt
new file mode 100644
index 0000000000..a608d4d64d
--- /dev/null
+++ b/challenge-002/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/620.html
diff --git a/challenge-002/adam-russell/perl5/ch-1.pl b/challenge-002/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..13aea40752
--- /dev/null
+++ b/challenge-002/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,7 @@
+use strict;
+use warnings;
+##
+# Write a script or one-liner to remove leading zeros from positive numbers.
+##
+use constant EXAMPLE => "000123";
+print sprintf("%d", EXAMPLE)."\n";
diff --git a/challenge-002/adam-russell/perl5/ch-2.pl b/challenge-002/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..e79ee038ef
--- /dev/null
+++ b/challenge-002/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,68 @@
+use strict;
+use warnings;
+##
+# Challenge #2
+# Write a script that can convert integers to and from a base35 representation, using the characters 0-9 and A-Y.
+##
+use constant BASE => 35;
+use constant OFFSET => 87;
+use constant EXAMPLE_0 => "7";
+use constant EXAMPLE_1 => "11";
+use constant EXAMPLE_2 => "1A7H";
+use constant EXAMPLE_3 => "7";
+use constant EXAMPLE_4 => "36";
+use constant EXAMPLE_5 => "55387";
+
+my(@digits,$converted);
+@digits = split(//,EXAMPLE_0);
+$converted = base35_to_base10(\@digits);
+print "(base 35 -> base 10) ". EXAMPLE_0 . " -> $converted\n";
+
+@digits = split(//,EXAMPLE_1);
+$converted = base35_to_base10(\@digits);
+print "(base 35 -> base 10) ". EXAMPLE_1 . " -> $converted\n";
+
+@digits = split(//,EXAMPLE_2);
+$converted = base35_to_base10(\@digits);
+print "(base 35 -> base 10) ". EXAMPLE_2 . " -> $converted\n";
+
+$converted = base10_to_base35(EXAMPLE_3);
+print "(base 10 -> base 35) ". EXAMPLE_3 . " -> $converted\n";
+
+$converted = base10_to_base35(EXAMPLE_4);
+print "(base 10 -> base 35) ". EXAMPLE_4 . " -> $converted\n";
+
+$converted = base10_to_base35(EXAMPLE_5);
+print "(base 10 -> base 35) ". EXAMPLE_5 . " -> $converted\n";
+
+sub base10_to_base35{
+ my $number = shift(@_);
+ if($number == 0){
+ return "";
+ }
+ my $value = $number % BASE;
+ $number = int($number / BASE);
+
+ $value = uc(chr($value + OFFSET)) if $value >= 10;
+ my $r = base10_to_base35($number);
+ return $r .= $value;
+}
+
+sub base35_to_base10{
+ my $digits = shift(@_);
+ my $length = @{$digits};
+ my $current_digit = shift(@{$digits});
+ my $value;
+ if($current_digit =~ m/\d/){
+ $value = $current_digit;
+ }
+ else{
+ $value = ord(lc($current_digit)) - OFFSET;
+ }
+ if($length == 1){
+ return $value;
+ }
+ else{
+ return $value * (BASE ** ($length-1)) + base35_to_base10($digits);
+ }
+}