aboutsummaryrefslogtreecommitdiff
path: root/challenge-002
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2019-12-17 16:45:28 -0600
committerRyan Thompson <i@ry.ca>2019-12-17 16:45:28 -0600
commita26d8716271b568c97c62ff33dafcc123fb1e33f (patch)
treee6dbea8dff7766376436c9d0200a971c6e2f8b08 /challenge-002
parenta3c82344c19bb927beed4b1b03a056d5e99db0ed (diff)
downloadperlweeklychallenge-club-a26d8716271b568c97c62ff33dafcc123fb1e33f.tar.gz
perlweeklychallenge-club-a26d8716271b568c97c62ff33dafcc123fb1e33f.tar.bz2
perlweeklychallenge-club-a26d8716271b568c97c62ff33dafcc123fb1e33f.zip
Solutions for challenges 001-005
Diffstat (limited to 'challenge-002')
-rw-r--r--challenge-002/ryan-thompson/README1
-rwxr-xr-xchallenge-002/ryan-thompson/perl5/ch-1.pl19
-rwxr-xr-xchallenge-002/ryan-thompson/perl5/ch-2.pl46
-rwxr-xr-xchallenge-002/ryan-thompson/perl6/ch-1.p69
-rwxr-xr-xchallenge-002/ryan-thompson/perl6/ch-2.p610
5 files changed, 85 insertions, 0 deletions
diff --git a/challenge-002/ryan-thompson/README b/challenge-002/ryan-thompson/README
new file mode 100644
index 0000000000..53b1e7cfa0
--- /dev/null
+++ b/challenge-002/ryan-thompson/README
@@ -0,0 +1 @@
+Solutions by Ryan Thompson.
diff --git a/challenge-002/ryan-thompson/perl5/ch-1.pl b/challenge-002/ryan-thompson/perl5/ch-1.pl
new file mode 100755
index 0000000000..6b39a9c09e
--- /dev/null
+++ b/challenge-002/ryan-thompson/perl5/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - Remove leading zeroes from positive numbers
+#
+# Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+
+# Accept filenames or standard input
+
+# The spec was a bit vague, so I'm choosing to only modify numbers that start
+# at the beginning of a line. Change the ^ to a \b to work on any number
+print s/^0+(?=[1-9])//r while (<<>>);
+
+# Another cheeky way about it, if you are certain you only have +ve ints:
+# say 0+$_ while (<<>>);
diff --git a/challenge-002/ryan-thompson/perl5/ch-2.pl b/challenge-002/ryan-thompson/perl5/ch-2.pl
new file mode 100755
index 0000000000..6f67afd439
--- /dev/null
+++ b/challenge-002/ryan-thompson/perl5/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+#
+# ch-2.pl - Convert integers to and from base35
+#
+# 2019 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+
+my @base35 = (0..9, 'a'..'y'); # Display representation
+my %val; @val{@base35} = 0..34;
+
+# Verification: Covert 20 random numbers to and from base35
+printf "%8d => %8s => %8d\n",
+ $_, int_to_base35($_), base35_to_int(int_to_base35($_))
+ for map { int rand(1e8) } 1..20;
+
+sub int_to_base35 {
+ my $int = shift;
+ die "Expecting integer" unless $int =~ /^\d+$/;
+
+ my $base35;
+ while ($int) {
+ $base35 = $base35[ $int % 35 ] . $base35;
+ my $digit = $int % 35;
+ $int = int($int / 35);
+ }
+
+ $base35 // 0;
+}
+
+sub base35_to_int {
+ my $base35 = lc( shift );
+ die "Expecting [0-9a-y]" unless $base35 =~ /^[0-9a-y]+$/;
+
+ my $mul = 1; # Digits multiplier
+ my $int = 0; # Result
+ for (reverse split '', $base35) {
+ $int += $mul * $val{$_};
+ $mul *= 35;
+ }
+
+ $int;
+}
diff --git a/challenge-002/ryan-thompson/perl6/ch-1.p6 b/challenge-002/ryan-thompson/perl6/ch-1.p6
new file mode 100755
index 0000000000..626b52cc1f
--- /dev/null
+++ b/challenge-002/ryan-thompson/perl6/ch-1.p6
@@ -0,0 +1,9 @@
+#!/usr/bin/env perl6
+
+# ch-1.p6 - Remove leading zeroes from positive numbers
+#
+# Ryan Thompson <rjt@cpan.org>
+
+use v6;
+
+S/^0+ <?[ 1..9 ]>//.say for $*ARGFILES.lines;
diff --git a/challenge-002/ryan-thompson/perl6/ch-2.p6 b/challenge-002/ryan-thompson/perl6/ch-2.p6
new file mode 100755
index 0000000000..10c65b3af8
--- /dev/null
+++ b/challenge-002/ryan-thompson/perl6/ch-2.p6
@@ -0,0 +1,10 @@
+#!/usr/bin/env perl6
+
+# ch-2.p6 - Convert numbers to/from base35
+#
+# Ryan Thompson <rjt@cpan.org>
+
+use v6;
+
+.say for <0 34 35 69 70 1170605>».base(35);
+.say for <0 Y 10 1Y 20 Raku>».parse-base(35);