diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-07 19:18:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-07 19:18:19 +0100 |
| commit | 1215e8707d1b1934c36f08a35a662c32bb7f6782 (patch) | |
| tree | 1c9282a351bcb34cf7569e9a36158705506930b1 | |
| parent | 5b2a0a8345bcbd0d11659b58a2bee039a248babb (diff) | |
| parent | e9b0f4ccec312e240f6ad8cfbcd423a9748c89f5 (diff) | |
| download | perlweeklychallenge-club-1215e8707d1b1934c36f08a35a662c32bb7f6782.tar.gz perlweeklychallenge-club-1215e8707d1b1934c36f08a35a662c32bb7f6782.tar.bz2 perlweeklychallenge-club-1215e8707d1b1934c36f08a35a662c32bb7f6782.zip | |
Merge pull request #27 from bracteatus/master
Perl Weekly Challenge 002
| -rw-r--r-- | challenge-002/jaime/README.md | 29 | ||||
| -rw-r--r-- | challenge-002/jaime/perl5/ch-1.pl | 15 | ||||
| -rw-r--r-- | challenge-002/jaime/perl5/ch-2.pl | 44 |
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-002/jaime/README.md b/challenge-002/jaime/README.md new file mode 100644 index 0000000000..986705b1d6 --- /dev/null +++ b/challenge-002/jaime/README.md @@ -0,0 +1,29 @@ +# Perl Weekly Challenge 002 +By Jaime; [@tortsnare on Twitter](https://twitter.com/tortsnare). + +Straightforward solutions in Perl 5 that use no imports. +The user selects a conversion and feeds in numbers, in a UNIX-like approach. + +# 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;}` + +# 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. + +## 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. +} |
