diff options
| -rwxr-xr-x | challenge-327/perlboy1967/perl/ch1.pl | 34 | ||||
| -rwxr-xr-x | challenge-327/perlboy1967/perl/ch2.pl | 39 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-327/perlboy1967/perl/ch1.pl b/challenge-327/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..3cc274747d --- /dev/null +++ b/challenge-327/perlboy1967/perl/ch1.pl @@ -0,0 +1,34 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 327 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-327#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Missing Integers +Submitted by: Mohammad Sajid Anwar + +You are given an array of n integers. + +Write a script to find all the missing integers in the range 1..n in the given array. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::MoreUtils qw(indexes); + +sub missingIntegers (@ints) { + my @i = (0) x @ints; + $i[$_ - 1]++ for (@ints); + map { $_ + 1 } indexes { $_ == 0 } @i; +} + +is([missingIntegers(1,2,1,3,2,5)],[4,6],'Example 1'); +is([missingIntegers(1,1,1)],[2,3],'Example 2'); +is([missingIntegers(2,2,1)],[3],'Example 3'); + +done_testing; diff --git a/challenge-327/perlboy1967/perl/ch2.pl b/challenge-327/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..787a3d3cc0 --- /dev/null +++ b/challenge-327/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 327 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-327#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: MAD +Submitted by: Mohammad Sajid Anwar + +You are given an array of distinct integers. + +Write a script to find all pairs of elements with minimum absolute difference +(MAD) of any two elements. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use Algorithm::Combinatorics qw(combinations); +use List::Util qw(min); + +sub getMad (@ints) { + my %ad; + for my $pair (combinations(\@ints,2)) { + my ($lo,$hi) = sort { $a <=> $b } @$pair; + push(@{$ad{$hi - $lo}},[$lo,$hi]); + } + sort {$$a[0] <=> $$b[0]} @{$ad{min keys %ad}}; +} + +is([getMad(4,1,2,3)],[[1,2],[2,3],[3,4]],'Example 1'); +is([getMad(1,3,7,11,15)],[[1,3]],'Example 2'); +is([getMad(1,5,3,8)],[[1,3],[3,5]],'Example 3'); + +done_testing; |
