aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-03 19:52:51 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-03 19:52:51 +0100
commit58d62224aa01b622eda8662cc30f007efac71167 (patch)
treecb228f442d860f448e0ef26ae844a64f5a51a5fd
parentb213013759f49650d5b2e57238a66d36b8400b70 (diff)
downloadperlweeklychallenge-club-58d62224aa01b622eda8662cc30f007efac71167.tar.gz
perlweeklychallenge-club-58d62224aa01b622eda8662cc30f007efac71167.tar.bz2
perlweeklychallenge-club-58d62224aa01b622eda8662cc30f007efac71167.zip
- Added solution by "Athanasius".
-rw-r--r--challenge-002/athanasius/perl5/ch-1.pl29
-rw-r--r--challenge-002/athanasius/perl5/ch-2.pl106
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-002/athanasius/perl5/ch-1.pl b/challenge-002/athanasius/perl5/ch-1.pl
new file mode 100644
index 0000000000..f831efa8d8
--- /dev/null
+++ b/challenge-002/athanasius/perl5/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+while (my $n = <DATA>)
+{
+ chomp $n;
+
+ if ($n =~ / ^ \+? \. \d+ $ /x ||
+ $n =~ s/ ^ (\+?) 0* (\d+ \.? \d*) $ /$1$2/x)
+ {
+ print "$n\n";
+ }
+ else
+ {
+ print "$n is not a positive number\n";
+ }
+}
+
+__DATA__
+00012345
++0678
+0000090.123
++00456.789
+0.123
+000.123
+0
+00
+.123
+-01
diff --git a/challenge-002/athanasius/perl5/ch-2.pl b/challenge-002/athanasius/perl5/ch-2.pl
new file mode 100644
index 0000000000..17e15af2f7
--- /dev/null
+++ b/challenge-002/athanasius/perl5/ch-2.pl
@@ -0,0 +1,106 @@
+use strict;
+use warnings;
+use Const::Fast;
+
+const my $ZERO => ord('0');
+const my $NINE => ord('9');
+const my $A => ord('A');
+const my $Y => ord('Y');
+const my @DIGITS => ('0' .. '9', 'A' .. 'Y');
+const my @POWERS => get_powers();
+
+while (my $n = <DATA>)
+{
+ chomp $n;
+ my $p = base35to10($n);
+ my $q = base10to35($p);
+ printf "%8s(35) = %11s(10) = %8s(35)\n", $n, $p, $q;
+}
+
+sub base35to10
+{
+ my ($base35) = @_;
+ my $base10 = 0;
+ my $power = 1;
+
+ for my $digit (reverse split //, $base35)
+ {
+ $base10 += digit35to10($digit) * $power;
+ $power *= 35;
+ }
+
+ return $base10;
+}
+
+sub base10to35
+{
+ my ($base10) = @_;
+
+ $base10 < $POWERS[-1] or die "Number $base10 too large, stopped";
+
+ my $index = get_highest_index($base10);
+ my $base35 = '';
+
+ for my $i (reverse 0 .. $index)
+ {
+ my $digit = $DIGITS[ int($base10 / $POWERS[$i]) ];
+ $base35 .= $digit;
+ $base10 %= $POWERS[$i];
+ }
+
+ return $base35;
+}
+
+sub get_highest_index
+{
+ my ($base10) = @_;
+ my $index = $#POWERS;
+
+ for my $i (reverse 1 .. $#POWERS - 1)
+ {
+ last if $base10 >= $POWERS[$i];
+ $index = $i - 1;
+ }
+
+ return $index;
+}
+
+sub digit35to10
+{
+ my ($digit) = @_;
+ my $ord = ord($digit);
+
+ return $digit if $ord >= $ZERO && $ord <= $NINE;
+ return ($ord - $A + 10) if $ord >= $A && $ord <= $Y;
+
+ die "Unknown base-35 digit '$digit', stopped";
+}
+
+sub get_powers
+{
+ my @powers;
+ my $power = 1;
+
+ for (1 .. 13)
+ {
+ push @powers, $power;
+ $power *= 35;
+ }
+
+ return @powers;
+}
+
+__DATA__
+10
+17
+35
+52
+A
+1A
+100
+ABC
+123
+36
+0
+13579BEH
+4