aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-24 23:57:39 +0100
committerGitHub <noreply@github.com>2025-06-24 23:57:39 +0100
commit56d38d532cb5ab2fef151cf8f4731e8f16c24421 (patch)
treecacdd5475820924ed150457faf9edf56b7189583
parentd45db4fe09a081919a9ecacaf421fcfc56725332 (diff)
parent3e0e7853105cfd811b5d685cf2a30531b6cfcf10 (diff)
downloadperlweeklychallenge-club-56d38d532cb5ab2fef151cf8f4731e8f16c24421.tar.gz
perlweeklychallenge-club-56d38d532cb5ab2fef151cf8f4731e8f16c24421.tar.bz2
perlweeklychallenge-club-56d38d532cb5ab2fef151cf8f4731e8f16c24421.zip
Merge pull request #12237 from jeanluc2020/jeanluc2020-327
Add solution 327.
-rw-r--r--challenge-327/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-327/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-327/jeanluc2020/perl/ch-1.pl57
-rwxr-xr-xchallenge-327/jeanluc2020/perl/ch-2.pl69
4 files changed, 128 insertions, 0 deletions
diff --git a/challenge-327/jeanluc2020/blog-1.txt b/challenge-327/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..c75d6062d1
--- /dev/null
+++ b/challenge-327/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-327-1.html
diff --git a/challenge-327/jeanluc2020/blog-2.txt b/challenge-327/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..d49a2a4b63
--- /dev/null
+++ b/challenge-327/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-327-2.html
diff --git a/challenge-327/jeanluc2020/perl/ch-1.pl b/challenge-327/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..633d617764
--- /dev/null
+++ b/challenge-327/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-327/#TASK1
+#
+# Task 1: Missing Integers
+# ========================
+#
+# 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)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We turn each element in the input into a key of a hash. Then
+# we walk from 1 to n, and if that number is not in the hash,
+# we simply add it to the output list.
+
+use v5.36;
+
+missing_integers(1, 2, 1, 3, 2, 5);
+missing_integers(1, 1, 1);
+missing_integers(2, 2, 1);
+
+sub missing_integers (@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my @output = ();
+ my %map = map { $_ => 1 } @ints;
+ foreach my $i (1..$#ints+1) {
+ push @output, $i unless $map{$i};
+ }
+ say "Output: (" . join(", ", @output) . ")";
+}
diff --git a/challenge-327/jeanluc2020/perl/ch-2.pl b/challenge-327/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..73e49f7379
--- /dev/null
+++ b/challenge-327/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-327/#TASK2
+#
+# Task 2: MAD
+# ===========
+#
+# 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]
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We take two walks of the array. First, we find the MAD by calculating
+# all absolute distances, keeping track of the minimum. Then, we keep all
+# pairs of numbers where the absolute distance matches the minimum. For
+# a nicer output, we sort the pairs that we keep by size, and in the end
+# we sort the output by size as well.
+
+use v5.36;
+
+mad(4, 1, 2, 3);
+mad(1, 3, 7, 11, 15);
+mad(1, 5, 3, 8);
+
+sub mad(@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my $mad = abs($ints[0] - $ints[1]);
+ my @output = ();
+ foreach my $i (0..$#ints) {
+ foreach my $j ($i+1..$#ints) {
+ my $ad = abs($ints[$i] - $ints[$j]);
+ $mad = $ad if $mad > $ad;
+ }
+ }
+ foreach my $i (0..$#ints) {
+ foreach my $j ($i+1..$#ints) {
+ my $ad = abs($ints[$i] - $ints[$j]);
+ if($ad == $mad) {
+ push @output, $ints[$i] > $ints[$j] ? [$ints[$j], $ints[$i]] : [$ints[$i], $ints[$j]];
+ }
+ }
+ }
+ say "Output: " . join(", ", map { "[$_->[0], $_->[1]]" } sort { $a->[0] <=> $b->[0] } @output);
+}