aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-15 23:10:13 +0100
committerGitHub <noreply@github.com>2025-09-15 23:10:13 +0100
commit7f78d729369f524a8bf4d7d9eb693aa689d78af6 (patch)
treee20150ce5ceb9402d9ac95a84b0d9e0e5aaa818c
parentde543de02dfe1576d5bb84ac062ba6c6cf745a06 (diff)
parent84bc59e80a33649a5fa9ab49d7abceb5ac94e0e0 (diff)
downloadperlweeklychallenge-club-7f78d729369f524a8bf4d7d9eb693aa689d78af6.tar.gz
perlweeklychallenge-club-7f78d729369f524a8bf4d7d9eb693aa689d78af6.tar.bz2
perlweeklychallenge-club-7f78d729369f524a8bf4d7d9eb693aa689d78af6.zip
Merge pull request #12684 from jeanluc2020/jeanluc2020-339
Add solution 339.
-rw-r--r--challenge-339/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-339/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-339/jeanluc2020/perl/ch-1.pl102
-rwxr-xr-xchallenge-339/jeanluc2020/perl/ch-2.pl108
4 files changed, 212 insertions, 0 deletions
diff --git a/challenge-339/jeanluc2020/blog-1.txt b/challenge-339/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..d178938ef8
--- /dev/null
+++ b/challenge-339/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-339-1.html
diff --git a/challenge-339/jeanluc2020/blog-2.txt b/challenge-339/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..7b2cfd97b0
--- /dev/null
+++ b/challenge-339/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-339-2.html
diff --git a/challenge-339/jeanluc2020/perl/ch-1.pl b/challenge-339/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..5cc7ed4953
--- /dev/null
+++ b/challenge-339/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-339/#TASK1
+#
+# Task 1: Max Diff
+# ================
+#
+# You are given an array of integers having four or more elements.
+#
+# Write a script to find two pairs of numbers from this list (four numbers
+# total) so that the difference between their products is as large as possible.
+#
+# In the end return the max difference.
+#
+## With Two pairs (a, b) and (c, d), the product difference is (a * b) - (c * d).
+#
+#
+## Example 1
+##
+## Input: @ints = (5, 9, 3, 4, 6)
+## Output: 42
+##
+## Pair 1: (9, 6)
+## Pair 2: (3, 4)
+## Product Diff: (9 * 6) - (3 * 4) => 54 - 12 => 42
+#
+#
+## Example 2
+##
+## Input: @ints = (1, -2, 3, -4)
+## Output: 10
+##
+## Pair 1: (1, -2)
+## Pair 2: (3, -4)
+#
+#
+## Example 3
+##
+## Input: @ints = (-3, -1, -2, -4)
+## Output: 10
+##
+## Pair 1: (-1, -2)
+## Pair 2: (-3, -4)
+#
+#
+## Example 4
+##
+## Input: @ints = (10, 2, 0, 5, 1)
+## Output: 50
+##
+## Pair 1: (10, 5)
+## Pair 2: (0, 1)
+#
+#
+## Example 5
+##
+## Input: @ints = (7, 8, 9, 10, 10)
+## Output: 44
+##
+## Pair 1: (10, 10)
+## Pair 2: (7, 8)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We pick all possible combinations of 4 numbers of the input. Then we
+# create all possible diffs for those 4 numbers which due to the symmetry
+# of the diff calculation and the possibility to use the absolute value of
+# the diff comes down to 3 different possibilities for any set of 4 numbers.
+# So we pick the max of those 3 numbers and the current max to find the
+# final result in the end.
+
+use v5.36;
+use List::Util qw(max);
+
+max_diff(5, 9, 3, 4, 6);
+max_diff(1, -2, 3, -4);
+max_diff(-3, -1, -2, -4);
+max_diff(10, 2, 0, 5, 1);
+max_diff(7, 8, 9, 10, 10);
+
+sub max_diff(@ints) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my $output = 0;
+ foreach my $i (0..$#ints) {
+ foreach my $j ($i+1..$#ints) {
+ foreach my $k ($j+1..$#ints) {
+ foreach my $l ($k+1..$#ints) {
+ $output = max($output,
+ abs($ints[$i]*$ints[$j] - $ints[$k]*$ints[$l]),
+ abs($ints[$i]*$ints[$k] - $ints[$j]*$ints[$l]),
+ abs($ints[$i]*$ints[$l] - $ints[$k]*$ints[$j])
+ );
+ }
+ }
+ }
+ }
+
+ say "Output: $output";
+}
diff --git a/challenge-339/jeanluc2020/perl/ch-2.pl b/challenge-339/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..5357ad21f1
--- /dev/null
+++ b/challenge-339/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,108 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-339/#TASK2
+#
+# Task 2: Peak Point
+# ==================
+#
+# You are given an array of altitude gain.
+#
+# Write a script to find the peak point gained.
+#
+## Example 1
+##
+## Input: @gain = (-5, 1, 5, -9, 2)
+## Output: 1
+##
+## start: 0
+## 1st change: 0 + (-5) = -5
+## 2nd change: -5 + 1 = -4
+## 3rd change: -4 + 5 = 1
+## 4th change: 1 + (-9) = -8
+## 5th change: -8 + 2 = -6
+##
+## max(0, -5, -4, 1, -8, -6) = 1
+#
+#
+## Example 2
+##
+## Input: @gain = (10, 10, 10, -25)
+## Output: 30
+##
+## start: 0
+## 1st change: 0 + 10 = 10
+## 2nd change: 10 + 10 = 20
+## 3rd change: 20 + 10 = 30
+## 4th change: 30 + (-25) = 5
+##
+## max(0, 10, 20, 30, 5) = 30
+#
+#
+## Example 3
+##
+## Input: @gain = (3, -4, 2, 5, -6, 1)
+## Output: 6
+##
+## start: 0
+## 1st change: 0 + 3 = 3
+## 2nd change: 3 + (-4) = -1
+## 3rd change: -1 + 2 = 1
+## 4th change: 1 + 5 = 6
+## 5th change: 6 + (-6) = 0
+## 6th change: 0 + 1 = 1
+##
+## max(0, 3, -1, 1, 6, 0, 1) = 6
+#
+#
+## Example 4
+##
+## Input: @gain = (-1, -2, -3, -4)
+## Output: 0
+##
+## start: 0
+## 1st change: 0 + (-1) = -1
+## 2nd change: -1 + (-2) = -3
+## 3rd change: -3 + (-3) = -6
+## 4th change: -6 + (-4) = -10
+##
+## max(0, -1, -3, -6, -10) = 0
+#
+#
+## Example 5
+##
+## Input: @gain = (-10, 15, 5)
+## Output: 10
+##
+## start: 0
+## 1st change: 0 + (-10) = -10
+## 2nd change: -10 + 15 = 5
+## 3rd change: 5 + 5 = 10
+##
+## max(0, -10, 5, 10) = 10
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just walk the gains, calculate the new altitude and keep
+# the maximum one.
+
+use v5.36;
+
+peak_point(-5, 1, 5, -9, 2);
+peak_point(10, 10, 10, -25);
+peak_point(3, -4, 2, 5, -6, 1);
+peak_point(-1, -2, -3, -4);
+peak_point(-10, 15, 5);
+
+sub peak_point( @gain ) {
+ say "Input: (" . join(", ", @gain) . ")";
+ my $max = 0;
+ my $current = 0;
+ foreach my $g (@gain) {
+ $current += $g;
+ $max = $current > $max ? $current : $max;
+ }
+ say "Output: $max";
+}