diff options
| author | andrezgz <andrezgz@gmail.com> | 2019-05-01 12:04:12 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2019-05-01 12:04:12 -0300 |
| commit | 8f2c026343bef59f05a52b2f1959f2a1fcf9ae4c (patch) | |
| tree | c6d883e90828f94891be2d3f361ff087de126147 | |
| parent | 40c3f78dd10dbc9f9e425d3e439a01b00bcc2d9a (diff) | |
| download | perlweeklychallenge-club-8f2c026343bef59f05a52b2f1959f2a1fcf9ae4c.tar.gz perlweeklychallenge-club-8f2c026343bef59f05a52b2f1959f2a1fcf9ae4c.tar.bz2 perlweeklychallenge-club-8f2c026343bef59f05a52b2f1959f2a1fcf9ae4c.zip | |
challenge-006 andrezgz solution
| -rw-r--r-- | challenge-006/andrezgz/perl5/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-006/andrezgz/perl5/ch-2.pl | 13 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-006/andrezgz/perl5/ch-1.pl b/challenge-006/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..b0843cca96 --- /dev/null +++ b/challenge-006/andrezgz/perl5/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-006/ +# Challenge #1 +# Create a script which takes a list of numbers from command line and print the same in the compact form. +# For example, if you pass "1,2,3,4,9,10,14,15,16" then it should print the compact form like "1-4,9,10,14-16" + +use strict; +use warnings; + + +die "Usage: ch-1.pl <numbers_list>" unless ($ARGV[0]); + +# Numbers are sorted in ascending order just in case +my @numbers = sort {$a <=> $b} split ',', $ARGV[0]; + +my ($first, $last) = (shift @numbers) x 2; + +foreach my $n (@numbers){ + if ($n - $last > 1) { + print compact_term($first, $last).','; + $first = $n; + } + $last = $n; +} + +print compact_term($first, $last).$/; + +# Returns a term for the compact form: a single number, two numbers (m,n) or a range (m-n) +sub compact_term { + my ($first, $last) = @_; + my $separator = ($last - $first == 1) ? ',' : '-'; + return ($last == $first) ? $first : $first.$separator.$last; +} diff --git a/challenge-006/andrezgz/perl5/ch-2.pl b/challenge-006/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..ffe30f9767 --- /dev/null +++ b/challenge-006/andrezgz/perl5/ch-2.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-006/ +# Challenge #2 +# Create a script to calculate Ramanujan's constant with at least 32 digits of precision. Find out more about it here. +# https://en.wikipedia.org/wiki/Heegner_number#Almost_integers_and_Ramanujan's_constant + +use strict; +use warnings; + +use Math::BigFloat qw/bpi/; + +print Math::BigFloat->new(163)->bsqrt->bmul(bpi)->bexp(32); |
