From 6f87a70f462850f3f50c5ce5c6e4a47e020f1af7 Mon Sep 17 00:00:00 2001 From: bracteatus <42359730+bracteatus@users.noreply.github.com> Date: Sun, 7 Apr 2019 12:08:21 -0600 Subject: Straightforward solutions to 002. --- challenge-002/jaime/README.md | 17 ++++++++------- challenge-002/jaime/perl5/ch-1.pl | 15 +++++++++++++ challenge-002/jaime/perl5/ch-2.pl | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 challenge-002/jaime/perl5/ch-1.pl create mode 100644 challenge-002/jaime/perl5/ch-2.pl diff --git a/challenge-002/jaime/README.md b/challenge-002/jaime/README.md index d9a13f0ca9..0d64808e2a 100644 --- a/challenge-002/jaime/README.md +++ b/challenge-002/jaime/README.md @@ -4,23 +4,26 @@ By Jaime; @tortsnare on Twitter. Straightforward solutions in Perl 5 that use no imports. The user selects a conversion and feeds in numbers, in a UNIX-like approach. -The faniest bit was an `eval` introduced at the end to toggle the conversion operation at runtime. - -# ch-1.pl +# perl5/ch-1.pl +Write a script or one-liner to remove leading zeros from positive numbers. +## Solution Run as a single shell one-liner: `perl -E 'while(<>) {s/^0+//;print;}' # interactively reads numbers` or pipe the numbers, example: `cat number_list | perl -E 'while(<>) {s/^0+//;print;}` -# ch-2.pl -Run as a Perl 5 script. +# perl5/ch-2.pl +Write a script that can convert integers to and from a base35 representation, +using the characters 0-9 and A-Y. -## Convert base10 into base35 +## Solution +Convert between base10 and base35 strings. +User flags toggle the conversion operation at runtime. +## Convert base10 into base35 `cat integer_list | perl ch-2.pl` ## Convert base35 into base10 - `cat base35_list | perl ch-2.pl --base35-to-int` diff --git a/challenge-002/jaime/perl5/ch-1.pl b/challenge-002/jaime/perl5/ch-1.pl new file mode 100644 index 0000000000..1b41004d75 --- /dev/null +++ b/challenge-002/jaime/perl5/ch-1.pl @@ -0,0 +1,15 @@ +# Challenge #1 +# +# Write a script or one-liner to remove leading zeros from positive numbers. + +## Using Perl 5: +while (<>) { # read positive number as lines from stdin + s/^0+//; # remove leading zeros from each line + print; # show updated number +} + +## as a single shell one-liner: +# $ perl -E 'while(<>) {s/^0+//;print;}' # interactively reads numbers + +# or pipe the numbers to format, example: +# $ cat numbers_list | perl -E 'while(<>) {s/^0+//;print;}' diff --git a/challenge-002/jaime/perl5/ch-2.pl b/challenge-002/jaime/perl5/ch-2.pl new file mode 100644 index 0000000000..36f048c064 --- /dev/null +++ b/challenge-002/jaime/perl5/ch-2.pl @@ -0,0 +1,44 @@ +# Challenge #2 +# +# Write a script that can convert integers to and from a base35 representation, +# using the characters 0-9 and A-Y. Dave Jacoby came up with nice description +# about base35, in case you needed some background. + +my @numnums = (0..9,'A'..'Y'); # map integers 0..34 into base35 #TODO use actual map instead, for reverse lookup. + +sub intTo35 { + # convert positive integer into base35 string. + return "0" if (0 == $_); # special case; log(0) is undefined. + + my @result = (); + for my $i (0..(int(log($_)/log(35)))) { # iterate from int(log_35($_)) to 0. + push @result, @numnums[$_ % 35]; # map value of respective power of 35. + $_ /= 35; # prepare for next power of 35. + } + + return join "", reverse @result; # string encoding $_ as base35. +} + +sub intFrom35 { + # convert positive base35 string into integer + my ($result,$p) = (0,0); + for my $d (reverse (split //)) { # match each digit to its respective power of 35. + my $e = 0; # TODO reverse map lookup would reduce this whole section. + for my $num (@numnums) { + last if ($d eq $num); # found respective int multiplier. + $e++; + } + $result += $e * (35**($p++)); # appraise base35 digit as int. + } + return $result; # int of decoded base35 string. +} + +# default to conversion from int into base35, +# flag --base35-to-int to convert base35 into int. +$_ = shift; +my $converter = ($_ eq "--base35-to-int") ? "intFrom35" : "intTo35"; +print "# read numbers (also negative) from stdin.\n# use --base-35-to-int to convert from base35 into int.\n"; +while(<>) { + chomp $_; # read positive or negative numbers as lines from stdin + print "$converter($_) = @{[ ( s/^-// ? -(eval $converter) : (eval $converter) ) ]}\n"; # run selected conversion, appending sign. +} -- cgit