aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAilbhe Tweedie <atweedie@protonmail.ch>2019-04-02 12:35:13 +0200
committerAilbhe Tweedie <atweedie@protonmail.ch>2019-04-03 01:08:20 +0200
commit91245b2c0f12df8f7d919350611bcbfe28396618 (patch)
tree180eccbeab8669cd62485a30bd03c487cf2e9bdb
parent2fb6c2e357732c020209ea130df2b96290a36844 (diff)
downloadperlweeklychallenge-club-91245b2c0f12df8f7d919350611bcbfe28396618.tar.gz
perlweeklychallenge-club-91245b2c0f12df8f7d919350611bcbfe28396618.tar.bz2
perlweeklychallenge-club-91245b2c0f12df8f7d919350611bcbfe28396618.zip
002-p5-02: write initial version of toBase35()
-rwxr-xr-xchallenge-002/ailbhe-tweedie/perl5/ch-02.pl54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
new file mode 100755
index 0000000000..ac6174c6bd
--- /dev/null
+++ b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+#
+# Solution to challenge #2 of the Perl Weekly Challenge 002.
+#
+# Task: Write a script that can convert integers to and from a base35
+# representation, using the characters 0-9 and A-Y.
+#
+# Prerequisites: cpan install Scalar::Util::Numeric
+
+use strict;
+use warnings;
+use Scalar::Util::Numeric qw(isint);
+use Data::Dump;
+
+my $BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY";
+
+while (<>) {
+ chomp;
+ my $input = $_;
+ next unless isint $input;
+ my $output = convertToBase35($input);
+ print "output: $output\n";
+}
+
+sub convertToBase35 {
+ 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;
+ }
+ while ($max > 0) {
+ my $exp = $max - 1;
+ 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";
+ }
+ push @convert, $place;
+ $max--;
+ }
+ dd @convert;
+ foreach my $b (@base) {
+ print "-$b-\n";
+ }
+ my $output = join "", @base[@convert];
+}