From 91245b2c0f12df8f7d919350611bcbfe28396618 Mon Sep 17 00:00:00 2001 From: Ailbhe Tweedie Date: Tue, 2 Apr 2019 12:35:13 +0200 Subject: 002-p5-02: write initial version of toBase35() --- challenge-002/ailbhe-tweedie/perl5/ch-02.pl | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 challenge-002/ailbhe-tweedie/perl5/ch-02.pl 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]; +} -- cgit