diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-07 11:12:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-07 11:12:24 +0100 |
| commit | f32b6621d9cee67d9e187881705598a93872d65f (patch) | |
| tree | ca9718e7c43af537ee013aadd1c845b9b377efe6 | |
| parent | 5ab40076185c0cce69b25c40836913de9aa570f2 (diff) | |
| parent | 6c57c87a3d216e0aebb093b5d419985b3570264e (diff) | |
| download | perlweeklychallenge-club-f32b6621d9cee67d9e187881705598a93872d65f.tar.gz perlweeklychallenge-club-f32b6621d9cee67d9e187881705598a93872d65f.tar.bz2 perlweeklychallenge-club-f32b6621d9cee67d9e187881705598a93872d65f.zip | |
Merge pull request #23 from ailbhe-tweedie/atweedie
Solutions for week 1 & 2
| -rw-r--r-- | challenge-001/ailbhe-tweedie/README | 1 | ||||
| -rwxr-xr-x | challenge-001/ailbhe-tweedie/perl5/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-002/ailbhe-tweedie/README | 1 | ||||
| -rwxr-xr-x | challenge-002/ailbhe-tweedie/perl5/ch-1.pl | 9 | ||||
| -rwxr-xr-x | challenge-002/ailbhe-tweedie/perl5/ch-2.pl | 107 |
5 files changed, 132 insertions, 0 deletions
diff --git a/challenge-001/ailbhe-tweedie/README b/challenge-001/ailbhe-tweedie/README new file mode 100644 index 0000000000..36d4034b70 --- /dev/null +++ b/challenge-001/ailbhe-tweedie/README @@ -0,0 +1 @@ +Solution by Ailbhe Tweedie diff --git a/challenge-001/ailbhe-tweedie/perl5/ch-1.pl b/challenge-001/ailbhe-tweedie/perl5/ch-1.pl new file mode 100755 index 0000000000..22c1d97107 --- /dev/null +++ b/challenge-001/ailbhe-tweedie/perl5/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +# +# Write a script to replace the character ‘e’ with ‘E’ in the string +# ‘Perl Weekly Challenge’. Also print the number of times the +# character ‘e’ is found in the string. + +my $string = 'Perl Weekly Challenge'; +my $numberOfEs; +while ($string =~ /\G[^e]*e/g) { + $numberOfEs++; +} +print "Es: $numberOfEs\n"; +my $replaced = $string =~ s/e/E/g; +print "replaced: $replaced"; diff --git a/challenge-002/ailbhe-tweedie/README b/challenge-002/ailbhe-tweedie/README new file mode 100644 index 0000000000..36d4034b70 --- /dev/null +++ b/challenge-002/ailbhe-tweedie/README @@ -0,0 +1 @@ +Solution by Ailbhe Tweedie diff --git a/challenge-002/ailbhe-tweedie/perl5/ch-1.pl b/challenge-002/ailbhe-tweedie/perl5/ch-1.pl new file mode 100755 index 0000000000..e4a72a65e9 --- /dev/null +++ b/challenge-002/ailbhe-tweedie/perl5/ch-1.pl @@ -0,0 +1,9 @@ +#!/usr/bin/env perl +# +# Solution to challenge #1 of the Perl Weekly Challenge 002. + +while (<>) { + chomp; + $_ =~ s/^0+//; + print "$_\n"; +} diff --git a/challenge-002/ailbhe-tweedie/perl5/ch-2.pl b/challenge-002/ailbhe-tweedie/perl5/ch-2.pl new file mode 100755 index 0000000000..84d904ee09 --- /dev/null +++ b/challenge-002/ailbhe-tweedie/perl5/ch-2.pl @@ -0,0 +1,107 @@ +#!/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 +# +# Usage: +# > echo {0.999} | ./ch-02.pl to +# +# Test with: +# > echo {0..99} | ./ch-02.pl to | awk '{print $3}' | ./ch-02.pl from +# +# TODO: add tests + +use strict; +use warnings; +use Scalar::Util::Numeric qw(isint); +use Data::Dump; + +use v5.10; + +# TODO: split the string right here into an array and don't keep the $BASE variable +my $BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXY"; + +exit 1 unless @ARGV == 1; +my $switch = $ARGV[0]; + +while (<STDIN>) { + chomp; + # TODO: find out how to stream input, instead of saving it to an array first + my @input = split; + for my $i (@input) { + my $output; + if ($switch eq "to") { + $output = toBase35($i); + } elsif ($switch eq "from") { + $output = fromBase35($i); + } else { + say "Please specify to/from"; + exit 2; + } + # `pretty' print output + printf "%-4s\t==>\t%-4s\n", $i, $output; + } +} +exit 0; + +sub toBase35 { + my $input = shift; + + my @base = split "", $BASE; + my @convert; + my $max = 0; + + # Figure out the highest power of the base we need by incrementing + # base^power until it is bigger than the input. The exponent we're + # looking for is 1 less. + while (1) { + last unless @base ** ++$max <= $input; + } + + # Figure out the number's place values in a similar way by subtracting + # base^power from the input until the input is smaller than the value + # we're subtracting from it. The place values are saved in the array @convert. + while ($max > 0) { + my $exp = $max - 1; + my $pow = @base ** $exp; + my $place; + while ($pow <= $input) { + $input -= $pow; + $place++; + } + push @convert, $place; + $max--; + } + + # Return our number in the new base, by indexing the @base array with + # the @convert array. + # TODO: figure out if this implict return is `bad style' + my $output = join "", @base[@convert]; +} + +sub fromBase35 { + my $input = shift; + + # We create a hash converting an alphanumeric character in the new base to + # a base10 value + # TODO: move this out of the function + my %hash; + my @base = split "", $BASE; + for my $i (0..@base-1) { + $hash{$base[$i]} = $i; + } + + my @based = split "", $input; # bad variable name + my $max = @based - 1; + my $output; + # TODO: A for loop would probably be more clear at this point. + while ($max >= 0) { + $output += ( @base ** $max ) * $hash{@based[@based - $max - 1]}; + $max--; + } + return $output; +} |
