aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-30 02:47:16 +0100
committerGitHub <noreply@github.com>2025-06-30 02:47:16 +0100
commitb276c9e1e42d64fa9bd68d95b8ceec0766427f81 (patch)
treecaa769fbccb0aa0d9b4e8d323bd0e9dd2af09a9d
parent1e6a4a6daaeb24c321de840c6b60d343c3167e7f (diff)
parentb54bbc71289fcd7fd6a4bc4f7ff88a68215a016e (diff)
downloadperlweeklychallenge-club-b276c9e1e42d64fa9bd68d95b8ceec0766427f81.tar.gz
perlweeklychallenge-club-b276c9e1e42d64fa9bd68d95b8ceec0766427f81.tar.bz2
perlweeklychallenge-club-b276c9e1e42d64fa9bd68d95b8ceec0766427f81.zip
Merge pull request #12252 from wanderdoc/master
PWC 327 (wanderdoc)
-rw-r--r--challenge-327/wanderdoc/perl/ch-1.pl53
-rw-r--r--challenge-327/wanderdoc/perl/ch-2.pl62
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-327/wanderdoc/perl/ch-1.pl b/challenge-327/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..0455e0a902
--- /dev/null
+++ b/challenge-327/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+
+Example 1
+
+Input: @ints = (1, 2, 1, 3, 2, 5)
+Output: (4, 6)
+
+The given array has 6 elements.
+So we are looking for integers in the range 1..6 in the given array.
+The missing integers: (4, 6)
+
+
+Example 2
+
+Input: @ints = (1, 1, 1)
+Output: (2, 3)
+
+
+Example 3
+
+Input: @ints = (2, 2, 1)
+Output: (3)
+
+
+Example 4
+
+Input: @ints = (2, 1)
+Output: ()
+=cut
+
+
+
+
+
+use Test2::V0 -no_srand => 1;
+is( [missing_integers(1, 2, 1, 3, 2, 5)], [4, 6], 'Example 1');
+is( [missing_integers(1, 1, 1)], [2, 3], 'Example 2');
+is( [missing_integers(2, 2, 1)], [3], 'Example 3');
+is( [missing_integers(2, 1)], [], 'Example 1');
+done_testing();
+
+sub missing_integers
+{
+ my @arr = @_;
+ my %lookup = map {$_ => undef} @arr;
+ return grep { not exists $lookup{$_} } 1 .. @arr;
+}
diff --git a/challenge-327/wanderdoc/perl/ch-2.pl b/challenge-327/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..3ede1b1a0c
--- /dev/null
+++ b/challenge-327/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+
+Example 1
+
+Input: @ints = (4, 1, 2, 3)
+Output: [1,2], [2,3], [3,4]
+
+The minimum absolute difference is 1.
+Pairs with MAD: [1,2], [2,3], [3,4]
+
+
+Example 2
+
+Input: @ints = (1, 3, 7, 11, 15)
+Output: [1,3]
+
+
+Example 3
+
+Input: @ints = (1, 5, 3, 8)
+Output: [1,3], [3,5]
+
+
+=cut
+
+
+use Test2::V0 -no_srand => 1;
+
+is([min_abs_diff(4, 1, 2, 3)], [[1,2], [2,3], [3,4]], 'Example 1');
+is([min_abs_diff(1, 3, 7, 11, 15)], [[1,3]], 'Example 2');
+is([min_abs_diff(1, 5, 3, 8)], [[1,3], [3,5]], 'Example 3');
+done_testing();
+
+
+
+sub min_abs_diff
+{
+ my @arr = @_;
+ @arr = sort { $a <=> $b } @arr;
+ my @output;
+ my $min_diff = ~0+1;
+ for my $idx ( 0 .. $#arr-1 )
+ {
+ if ( ($arr[$idx+1] - $arr[$idx]) < $min_diff )
+ {
+ @output = ();
+ push @output, [$arr[$idx], $arr[$idx+1]];
+ $min_diff = $arr[$idx+1] - $arr[$idx]
+ }
+ elsif (($arr[$idx+1] - $arr[$idx]) == $min_diff)
+ {
+ push @output, [$arr[$idx], $arr[$idx+1]];
+ }
+ }
+ return @output;
+}