diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-03 20:27:46 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-03 20:27:46 +0100 |
| commit | d9585a60963451a628d3d2bcb4e1dc5d4916fd44 (patch) | |
| tree | 11617ab99597df6b10f8b16e12ed2e2b466190d4 | |
| parent | 8f2a9c584fd921037ccfb8ec304865dae2d52796 (diff) | |
| download | perlweeklychallenge-club-d9585a60963451a628d3d2bcb4e1dc5d4916fd44.tar.gz perlweeklychallenge-club-d9585a60963451a628d3d2bcb4e1dc5d4916fd44.tar.bz2 perlweeklychallenge-club-d9585a60963451a628d3d2bcb4e1dc5d4916fd44.zip | |
- Added solution by "Jo Christian Oterhals".
5 files changed, 55 insertions, 0 deletions
diff --git a/challenge-002/jo-christian-oterhals/perl5/base35.pl b/challenge-002/jo-christian-oterhals/perl5/base35.pl new file mode 100644 index 0000000000..921b84bd0c --- /dev/null +++ b/challenge-002/jo-christian-oterhals/perl5/base35.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +# +# Filename: base35.pl +# +# Usage: +# perl base35.pl [+-]NUMBER FROM-BASE, e.g. +# +# perl base35.pl 1000 10 +# Output: SK +# +# perl base35.pl SK 35 +# Output: 1000 +# +# perl base35.pl -SK 35 +# Output: -1000 +use v5.18; +say base35_conv(@ARGV); +sub base35_conv { + my ($no, $base) = (uc(shift), shift); + if ($base != 10 && $base != 35) { + warn "Not a valid base, must be 10 or 35"; + return -1; + } + if (($base == 35 && $no !~ /^[\+\-]{0,1}[0-9A-Y]+$/) || ($base == 10 && $no !~ /^[\+\-]{0,1}[0-9]+$/)) { + warn "You have to provide a valid number for the given base"; + return -1; + } + my ($c, $e) = (0, 0); + my $prefix = $no =~ s/^(\+|-)// ? $1 : ""; + my %d = map { if ($base == 35) { $_ => $c++ } else { $c++ => $_ } } (0..9,'A'..'Y'); + if ($base == 35) { + my $i = 1; + for (reverse(split("", $no))) { + $e += $i * $d{$_}; + $i = $i * 35; + } + } + else { + my @digits; + while ($no > 0) { + push @digits, $d{$no % 35}; + $no = int($no / 35); + } + $e = join("", reverse(@digits)); + } + return ( $prefix ? $prefix : "" ) . $e; +} + + diff --git a/challenge-002/jo-christian-oterhals/perl5/ch-1.sh b/challenge-002/jo-christian-oterhals/perl5/ch-1.sh new file mode 100644 index 0000000000..adaa66e050 --- /dev/null +++ b/challenge-002/jo-christian-oterhals/perl5/ch-1.sh @@ -0,0 +1 @@ +perl -E 'say "001000"*1;' diff --git a/challenge-002/jo-christian-oterhals/perl5/ch-2.sh b/challenge-002/jo-christian-oterhals/perl5/ch-2.sh new file mode 100644 index 0000000000..757fdd3fd1 --- /dev/null +++ b/challenge-002/jo-christian-oterhals/perl5/ch-2.sh @@ -0,0 +1,3 @@ +perl -E '%d = map { $_ => $c++ } (0..9,A..Y); $i = 1; for (reverse(split("", @ARGV[0]))) { $e += $i * $d{$_}; $i = $i * 35; } say $e' 1M5 + +perl -E '%d = map { $c++ => $_ } (0..9,A..Y); while ($ARGV[0] > 0) { push @n, $d{$ARGV[0] % 35}; $ARGV[0] = int($ARGV[0] / 35); } say join("", reverse(@n));' 2000 diff --git a/challenge-002/jo-christian-oterhals/perl6/ch-1.sh b/challenge-002/jo-christian-oterhals/perl6/ch-1.sh new file mode 100644 index 0000000000..e254f8b3f2 --- /dev/null +++ b/challenge-002/jo-christian-oterhals/perl6/ch-1.sh @@ -0,0 +1 @@ +perl6 -e 'say "-001000"*1;' diff --git a/challenge-002/jo-christian-oterhals/perl6/ch-2.sh b/challenge-002/jo-christian-oterhals/perl6/ch-2.sh new file mode 100644 index 0000000000..72476f64e0 --- /dev/null +++ b/challenge-002/jo-christian-oterhals/perl6/ch-2.sh @@ -0,0 +1 @@ +perl6 -e 'say "1M5".parse-base(35)' |
