diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-08 11:32:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-08 11:32:47 +0100 |
| commit | f9413cec0b1c2c9532bf975294011e1276fff97d (patch) | |
| tree | 40c24bb6ed42166fa2e3a5b49b6c8e219eacade1 | |
| parent | 5bb9bdb177ed4dc8ff9805e28fccb75d733de496 (diff) | |
| parent | d3a46ab308b9d80311182fc3252de4984cba74d1 (diff) | |
| download | perlweeklychallenge-club-f9413cec0b1c2c9532bf975294011e1276fff97d.tar.gz perlweeklychallenge-club-f9413cec0b1c2c9532bf975294011e1276fff97d.tar.bz2 perlweeklychallenge-club-f9413cec0b1c2c9532bf975294011e1276fff97d.zip | |
Merge pull request #2236 from shasank-shah/perlchallenge-branch
My first commit
| -rw-r--r-- | challenge-002/shasank-shah/README | 1 | ||||
| -rw-r--r-- | challenge-002/shasank-shah/perl5/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-002/shasank-shah/perl5/ch-2.pl | 46 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-002/shasank-shah/README b/challenge-002/shasank-shah/README new file mode 100644 index 0000000000..3d2d911c6e --- /dev/null +++ b/challenge-002/shasank-shah/README @@ -0,0 +1 @@ +Solution by Shasank Shah diff --git a/challenge-002/shasank-shah/perl5/ch-1.pl b/challenge-002/shasank-shah/perl5/ch-1.pl new file mode 100644 index 0000000000..e9223cac3b --- /dev/null +++ b/challenge-002/shasank-shah/perl5/ch-1.pl @@ -0,0 +1,17 @@ +#! /usr/bin/perl +use v5.22; +say 'Casting with addition'; +print 'Number? '; +say 0 + <>; + +say 'Regex substitution'; +print 'Number? '; +say <> =~ s/^0*(\d+)$/$1/r; + +say 'Explicit integer casting'; +print 'Number? '; +say int <>; + +say 'Using map and a flip flop'; +print 'Number? '; +say join '', map { /[^0]/ .. /\n/ ? $_ : () } split //, <>; diff --git a/challenge-002/shasank-shah/perl5/ch-2.pl b/challenge-002/shasank-shah/perl5/ch-2.pl new file mode 100644 index 0000000000..1d67909982 --- /dev/null +++ b/challenge-002/shasank-shah/perl5/ch-2.pl @@ -0,0 +1,46 @@ +#! /usr/bin/perl +use v5.22; +my @to_table = ( 0 .. 9, 'A' .. 'Y'); +my %from_table; +while (my ($index, $element) = each @to_table) { + $from_table{$element} = $index; +} + +sub convert_to { + my $x = shift; my $pow = 1; my @ret; + do { + my $digit = $x % 35; + unshift @ret, $to_table[$digit]; + $x -= $digit; + $x /= 35; + } while $x > 0; + return join '', @ret +} + +sub convert_from { + my $x; my $pow =1; + for (reverse split //, shift) { + $x += $from_table{$_} * $pow; + $pow *= 35; + } + return $x +} + +my $command = shift; +if ($command eq 'to') { + my $arg = shift; + die 'Need a number to convert' unless $arg; + die 'Improper base10 number' unless $arg =~ /^\d+$/; + say convert_to($arg) +} elsif ($command eq 'from') { + my $arg = shift; + die 'Need a number to convert' unless $arg; + die 'Improper base35 number' unless $arg =~ /^[0-9A-Y]+$/; + say convert_from($arg) +} else { + say <<"EOF" + USAGE: $0 [ to | from ] + to - will convert a base10 number to base35 + from - will convert a base35 number to base10 +EOF +} |
