diff options
| -rwxr-xr-x | challenge-201/peter-meszaros/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-201/peter-meszaros/perl/ch-2.pl | 60 |
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-201/peter-meszaros/perl/ch-1.pl b/challenge-201/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..6d07d7d1c2 --- /dev/null +++ b/challenge-201/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Missing Numbers + +Submitted by: Mohammad S Anwar + +You are given an array of unique numbers. + +Write a script to find out all missing numbers in the range 0..$n where $n is +the array size. + +=head2 Example 1 + + Input: @array = (0,1,3) + Output: 2 + + The array size i.e. total element count is 3, so the range is 0..3. + The missing number is 2 in the given array. + +=head2 Example 2 + + Input: @array = (0,1) + Output: 2 + + The array size is 2, therefore the range is 0..2. + The missing number is 2. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[0, 1, 3], [2], 'Example 1'], + [[0, 1], [2], 'Example 2'], + [[0, 2, 4], [1, 3], 'Example 3'], +]; + +sub missing_numbers +{ + my $l = shift; + + my $n = @$l; + my %h = map {$_ => 1} 0 .. $n; + + delete @h{@$l}; + + return [sort {$a <=> $b} keys %h]; +} + +for (@$cases) { + is(missing_numbers($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-201/peter-meszaros/perl/ch-2.pl b/challenge-201/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..7c6b118bd0 --- /dev/null +++ b/challenge-201/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Penny Piles + +Submitted by: Robbie Hatley + +You are given an integer, $n > 0. + +Write a script to determine the number of ways of putting $n pennies in a row +of piles of ascending heights from left to right. + +=head2 Example + + Input: $n = 5 + Output: 7 + + Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: + + 1 1 1 1 1 + 1 1 1 2 + 1 2 2 + 1 1 3 + 2 3 + 1 4 + 5 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use Algorithm::Combinatorics qw/partitions/; +use Digest::SHA qw/sha512_hex/; + +my $cases = [ + [5, 7], +]; + +sub penny_piles +{ + my $n = shift; + + my @data = (1) x $n; + my %h; + my $iter = partitions(\@data); + while (my $p = $iter->next) { + my @p = sort {$a <=> $b} map { scalar @$_ } @$p; + ++$h{sha512_hex(join('-', @p))}; + } + + return scalar keys %h; +} + +for (@$cases) { + is(penny_piles($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
