diff options
| -rw-r--r-- | challenge-172/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-172/paulo-custodio/perl/ch-1.pl | 92 | ||||
| -rw-r--r-- | challenge-172/paulo-custodio/perl/ch-2.pl | 43 | ||||
| -rw-r--r-- | challenge-172/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-172/paulo-custodio/t/test-2.yaml | 5 |
5 files changed, 152 insertions, 0 deletions
diff --git a/challenge-172/paulo-custodio/Makefile b/challenge-172/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-172/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-172/paulo-custodio/perl/ch-1.pl b/challenge-172/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..6753187f1a --- /dev/null +++ b/challenge-172/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +# Challenge 172 +# +# Task 1: Prime Partition +# Submitted by: Mohammad S Anwar +# +# You are given two positive integers, $m and $n. +# +# Write a script to find out the Prime Partition of the given number. No +# duplicates allowed. +# +# For example, +# +# Input: $m = 18, $n = 2 +# Output: 5, 13 or 7, 11 +# +# Input: $m = 19, $n = 3 +# Output: 3, 5, 11 + +use Modern::Perl; +use List::Util 'sum'; + +sub is_prime { + my($n) = @_; + return 0 if $n <= 1; + return 1 if $n <= 3; + return 0 if ($n % 2)==0 || ($n % 3)==0; + for (my $i = 5; $i*$i <= $n; $i += 6) { + return 0 if ($n % $i)==0 || ($n % ($i+2))==0; + } + return 1; +} + +sub next_prime { + my($n) = @_; + return 2 if $n <= 1; + my $p = $n; + 1 while !is_prime(++$p); + return $p; +} + +sub primes { + my($n) = @_; + my $p = 2; + my @primes; + while ($p <= $n) { + push @primes, $p; + $p = next_prime($p); + } + return @primes; +} + +sub odometer_increment { + my($limit, @n) = @_; + my $i = $#n; + while (++$n[$i] >= $limit) { + $n[$i--] = 0; + if ($i < 0) { + unshift @n, 0; + $i = 0; + } + } + return @n; +} + +sub has_duplicates { + my(@n) = @_; + my %seen; + for (@n) { + return 1 if $seen{$_}++; + } + return 0; +} + +sub prime_partition { + my($m, $n) = @_; + my @primes = primes($m); + my @idx = (0) x $n; + while (@idx == $n) { + if (!has_duplicates(@idx)) { + my @terms = @primes[@idx]; + return @terms if sum(@terms) == $m; + } + @idx = odometer_increment(scalar(@primes), @idx); + } + return (); +} + +@ARGV==2 or die "usage: ch-1.pl m n\n"; +my($m, $n) = @ARGV; +say join ", ", prime_partition($m, $n); diff --git a/challenge-172/paulo-custodio/perl/ch-2.pl b/challenge-172/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..a7a6852c2e --- /dev/null +++ b/challenge-172/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Challenge 172 +# +# Task 2: Five-number Summary +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# +# Write a script to compute the five-number summary of the given set of integers. +# +# You can find the definition and example in the wikipedia page. + +use Modern::Perl; + +sub median { + my(@n) = @_; + if (@n % 2 == 0) { + my $i = @n / 2; + return ($n[$i-1] + $n[$i]) / 2; + } + else { + return $n[int(@n / 2)]; + } +} + +sub lower_quartile { + my(@n) = @_; + return median(@n[0..int(@n/2)-1]); +} + +sub upper_quartile { + my(@n) = @_; + return median(@n[int(@n/2)..$#n]); +} + +sub five_number_summary { + my(@n) = @_; + @n = sort {$a<=>$b} @n; + return ($n[0], lower_quartile(@n), median(@n), upper_quartile(@n), $n[-1]); +} + +say join ", ", five_number_summary(@ARGV); diff --git a/challenge-172/paulo-custodio/t/test-1.yaml b/challenge-172/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..7c06ae9ef9 --- /dev/null +++ b/challenge-172/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 18 2 + input: + output: 5, 13 +- setup: + cleanup: + args: 19 3 + input: + output: 3, 5, 11 diff --git a/challenge-172/paulo-custodio/t/test-2.yaml b/challenge-172/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..7271a491ad --- /dev/null +++ b/challenge-172/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 0 0 1 2 63 61 27 13 + input: + output: 0, 0.5, 7.5, 44, 63 |
