diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-27 21:51:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-27 21:51:29 +0000 |
| commit | 9a885bb57ce6f93df9543b7cbf498c334a16127b (patch) | |
| tree | e4f03dc11fa2ef31e4655bc944181bd1dbab928b | |
| parent | 2695fc28f35a2d6cf1e79b19c5b9c299844afe3b (diff) | |
| parent | 70ac653e352ff54a3f2e36050a550034ee594e63 (diff) | |
| download | perlweeklychallenge-club-9a885bb57ce6f93df9543b7cbf498c334a16127b.tar.gz perlweeklychallenge-club-9a885bb57ce6f93df9543b7cbf498c334a16127b.tar.bz2 perlweeklychallenge-club-9a885bb57ce6f93df9543b7cbf498c334a16127b.zip | |
Merge pull request #3088 from pauloscustodio/002-perl
Add Perl solution to challenge 002
| -rw-r--r-- | challenge-002/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/perl/ch-2.pl | 55 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/test.pl | 32 |
4 files changed, 102 insertions, 0 deletions
diff --git a/challenge-002/paulo-custodio/README b/challenge-002/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-002/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-002/paulo-custodio/perl/ch-1.pl b/challenge-002/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..cbe49c0da7 --- /dev/null +++ b/challenge-002/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +# Challenge 002 +# +# Challenge #1 +# Write a script or one-liner to remove leading zeros from positive numbers. + +use strict; +use warnings; +use 5.030; + +my($N) = @ARGV; +$N =~ s/^0+//; +say $N; diff --git a/challenge-002/paulo-custodio/perl/ch-2.pl b/challenge-002/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..db37e40e4c --- /dev/null +++ b/challenge-002/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +# Challenge 002 +# +# 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. + +use strict; +use warnings; +use 5.030; +use Getopt::Std; + +my @digits = ('0'..'9','A'..'Z'); + +our $opt_r; +(getopts('r') && @ARGV==1) or die "Usage: ch-2.pl [-r] number\n"; +if ($opt_r) { + say scan_base($ARGV[0], 35); +} +else { + say print_base($ARGV[0], 35); +} + + +sub print_base { + my($n, $base) = @_; + my $negative = ($n < 0); + $n = abs($n); + my $output = ''; + do { + my $d = $n % $base; + $n = int($n / $base); + $output = $digits[$d].$output; + } while ($n > 0); + $output = "-".$output if $negative; + return $output; +} + +sub scan_base { + my($str, $base) = @_; + my $n = 0; + my $negative; + $negative = 1 if $str =~ s/^-//; + while ($str =~ s/^(\w)//) { + my $c = $1; + my $d = ($c =~ /\d/) ? 0+$c : ord(uc($c))-ord('A')+10; + $d < $base or die "cannot parse $c$str\n"; + $n = $base * $n + $d; + } + $str eq '' or die "cannot parse $str\n"; + $n = -$n if $negative; + return $n; +} + +
\ No newline at end of file diff --git a/challenge-002/paulo-custodio/test.pl b/challenge-002/paulo-custodio/test.pl new file mode 100644 index 0000000000..c322545716 --- /dev/null +++ b/challenge-002/paulo-custodio/test.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use 5.030; + +is capture("perl perl/ch-1.pl 000123"), "123\n"; +is capture("perl perl/ch-1.pl 123"), "123\n"; + +is capture("perl perl/ch-2.pl 0"), "0\n"; +is capture("perl perl/ch-2.pl 1"), "1\n"; +is capture("perl perl/ch-2.pl 10"), "A\n"; +is capture("perl perl/ch-2.pl 34"), "Y\n"; +is capture("perl perl/ch-2.pl 35"), "10\n"; +is capture("perl perl/ch-2.pl -- -35"), "-10\n"; + +is capture("perl perl/ch-2.pl -r 0"), "0\n"; +is capture("perl perl/ch-2.pl -r 1"), "1\n"; +is capture("perl perl/ch-2.pl -r A"), "10\n"; +is capture("perl perl/ch-2.pl -r Y"), "34\n"; +is capture("perl perl/ch-2.pl -r 10"), "35\n"; +is capture("perl perl/ch-2.pl -r -- -10"), "-35\n"; + +done_testing; + +sub capture { + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \t\v\f\r]*\n/\n/g; + return $out; +} |
