From 5b856d3e5a2e50d41768731a3060d5d806e3c584 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 23 Jun 2025 08:14:30 +0000 Subject: w327 - Task 1 & 2 (Perl) --- challenge-327/perlboy1967/perl/ch1.pl | 34 ++++++++++++++++++++++++++++++ challenge-327/perlboy1967/perl/ch2.pl | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 challenge-327/perlboy1967/perl/ch1.pl create mode 100755 challenge-327/perlboy1967/perl/ch2.pl 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 + +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..008d5c34f1 --- /dev/null +++ b/challenge-327/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 327 +L + +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)) { + $pair = [sort { $a <=> $b } @$pair]; + push(@{$ad{$pair->[1] - $pair->[0]}},$pair); + } + 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; -- cgit From 1f01a9e8413c4245870e165d60c0068e08b058d5 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 23 Jun 2025 08:35:26 +0000 Subject: Task 2 _ A bit more readable --- challenge-327/perlboy1967/perl/ch2.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-327/perlboy1967/perl/ch2.pl b/challenge-327/perlboy1967/perl/ch2.pl index 008d5c34f1..787a3d3cc0 100755 --- a/challenge-327/perlboy1967/perl/ch2.pl +++ b/challenge-327/perlboy1967/perl/ch2.pl @@ -26,8 +26,8 @@ use List::Util qw(min); sub getMad (@ints) { my %ad; for my $pair (combinations(\@ints,2)) { - $pair = [sort { $a <=> $b } @$pair]; - push(@{$ad{$pair->[1] - $pair->[0]}},$pair); + my ($lo,$hi) = sort { $a <=> $b } @$pair; + push(@{$ad{$hi - $lo}},[$lo,$hi]); } sort {$$a[0] <=> $$b[0]} @{$ad{min keys %ad}}; } -- cgit