diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-24 23:31:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-24 23:31:04 +0100 |
| commit | 644db7f4e3e73513465388e1a57d973bb98d3a35 (patch) | |
| tree | 261e1c57daf02a0d3cb1e44c6fec5f9e22aa1ef0 | |
| parent | 2c405c527929f1de37f5aa415efca788a3e24221 (diff) | |
| parent | 1f01a9e8413c4245870e165d60c0068e08b058d5 (diff) | |
| download | perlweeklychallenge-club-644db7f4e3e73513465388e1a57d973bb98d3a35.tar.gz perlweeklychallenge-club-644db7f4e3e73513465388e1a57d973bb98d3a35.tar.bz2 perlweeklychallenge-club-644db7f4e3e73513465388e1a57d973bb98d3a35.zip | |
Merge pull request #12226 from PerlBoy1967/branch-for-challenge-327
w327 - Task 1 & 2 (Perl)
| -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; |
