From 6c57c87a3d216e0aebb093b5d419985b3570264e Mon Sep 17 00:00:00 2001 From: Ailbhe Tweedie Date: Sun, 7 Apr 2019 11:57:31 +0200 Subject: remove 0-padding from filenames --- challenge-001/ailbhe-tweedie/perl5/ch-01.pl | 14 ---- challenge-001/ailbhe-tweedie/perl5/ch-1.pl | 14 ++++ challenge-002/ailbhe-tweedie/perl5/ch-01.pl | 9 --- challenge-002/ailbhe-tweedie/perl5/ch-02.pl | 107 ---------------------------- challenge-002/ailbhe-tweedie/perl5/ch-1.pl | 9 +++ challenge-002/ailbhe-tweedie/perl5/ch-2.pl | 107 ++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+), 130 deletions(-) delete mode 100755 challenge-001/ailbhe-tweedie/perl5/ch-01.pl create mode 100755 challenge-001/ailbhe-tweedie/perl5/ch-1.pl delete mode 100755 challenge-002/ailbhe-tweedie/perl5/ch-01.pl delete mode 100755 challenge-002/ailbhe-tweedie/perl5/ch-02.pl create mode 100755 challenge-002/ailbhe-tweedie/perl5/ch-1.pl create mode 100755 challenge-002/ailbhe-tweedie/perl5/ch-2.pl diff --git a/challenge-001/ailbhe-tweedie/perl5/ch-01.pl b/challenge-001/ailbhe-tweedie/perl5/ch-01.pl deleted file mode 100755 index 22c1d97107..0000000000 --- a/challenge-001/ailbhe-tweedie/perl5/ch-01.pl +++ /dev/null @@ -1,14 +0,0 @@ -#!/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-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/perl5/ch-01.pl b/challenge-002/ailbhe-tweedie/perl5/ch-01.pl deleted file mode 100755 index e4a72a65e9..0000000000 --- a/challenge-002/ailbhe-tweedie/perl5/ch-01.pl +++ /dev/null @@ -1,9 +0,0 @@ -#!/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-02.pl b/challenge-002/ailbhe-tweedie/perl5/ch-02.pl deleted file mode 100755 index 84d904ee09..0000000000 --- a/challenge-002/ailbhe-tweedie/perl5/ch-02.pl +++ /dev/null @@ -1,107 +0,0 @@ -#!/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 () { - 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; -} 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 () { + 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; +} -- cgit