diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-04 13:03:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-04 13:03:39 +0100 |
| commit | 6af4da3c0647884da84538d52f6745bb657648ab (patch) | |
| tree | 18196538c1a51ecd293c87f6fc63e87fe00544e7 /challenge-002 | |
| parent | 8fc8c9c94611f3b41337b6413aaeddf9dbca92f7 (diff) | |
| parent | 2d315d3bcecf214960ec1a9a8db89eed885b0714 (diff) | |
| download | perlweeklychallenge-club-6af4da3c0647884da84538d52f6745bb657648ab.tar.gz perlweeklychallenge-club-6af4da3c0647884da84538d52f6745bb657648ab.tar.bz2 perlweeklychallenge-club-6af4da3c0647884da84538d52f6745bb657648ab.zip | |
Merge pull request #16 from andrezgz/challenge-002
Solution challenge-002 andrezgz
Diffstat (limited to 'challenge-002')
| -rw-r--r-- | challenge-002/andrezgz/README | 1 | ||||
| -rw-r--r-- | challenge-002/andrezgz/perl5/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-002/andrezgz/perl5/ch-2.pl | 52 |
3 files changed, 94 insertions, 0 deletions
diff --git a/challenge-002/andrezgz/README b/challenge-002/andrezgz/README new file mode 100644 index 0000000000..f4fd0da88e --- /dev/null +++ b/challenge-002/andrezgz/README @@ -0,0 +1 @@ +Solution by Andrezgz diff --git a/challenge-002/andrezgz/perl5/ch-1.pl b/challenge-002/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..36addeca96 --- /dev/null +++ b/challenge-002/andrezgz/perl5/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-002/ +# Challenge #1 +# Write a script or one-liner to remove leading zeros from positive numbers. + +# Wikipedia - https://en.wikipedia.org/wiki/Leading_zero +# A leading zero is any 0 digit that comes before the first nonzero digit +# in a number string in positional notation. +# When leading zeros occupy the most significant digits of an integer, +# they could be left blank or omitted for the same numeric value. +# Therefore, the usual decimal notation of integers does not use leading zeros +# except for the zero itself, which would be denoted as an empty string otherwise. + +# Non-numeric data +# 0a -> 0a +# a -> a + +# Non-positive numbers +# 0 -> 0 +# 0. -> 0. +# 0.0 -> 0.0 +# 00 -> 00 +# 00. -> 00. +# 00.0 -> 00.0 +# -01 -> -01 +# -01. -> -01. +# -01.0 -> -01.0 + +# Leading zeros on positive numbers +# 01 -> 1 +# 01. -> 1. +# 01.0 -> 1.0 +# 010.0 -> 10.0 + +use strict; + +my $number = @ARGV[0]; + +$number =~ s/^0+([1-9][0-9]*(:?[.,]\d*)?)$/$1/; +print $number; diff --git a/challenge-002/andrezgz/perl5/ch-2.pl b/challenge-002/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..0f005d4553 --- /dev/null +++ b/challenge-002/andrezgz/perl5/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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. +# https://gist.github.com/jacoby/764bb4e8a5d3a819b5fbfa497fcb3454 + +#Usage +# ch-2.pl --to-base35 <base-10-number> +# ch-2.pl --from-base35 <base-35-number> + +use strict; + +my @base = (0..9,'A' .. 'Y'); + +print to_base35($ARGV[1]) if ($ARGV[0] eq '--to-base35'); +print from_base35($ARGV[1]) if ($ARGV[0] eq '--from-base35'); + + +sub to_base35 { + my ($i) = @_; + + return 0 if ($i == 0); + + my $sign = ($i =~ s/-//) ? '-' : ''; + + my $result; + + while ($i > 0) { + $result .= $base[$i % @base]; + $i = int($i / @base); + } + return $sign . reverse $result; +} + +sub from_base35 { + my ($t) = @_; + $t = reverse $t; + + my $sign = ($t =~ s/-//) ? '-' : ''; + + my $result = 0; + + for my $i (0..length($t)-1){ + my $c = substr($t, $i, 1); + my ($index) = grep { $base[$_] eq $c } (0 .. @base-1); + $result += $index * ( @base ** $i ); + } + return $sign . $result; +} |
