aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-15 23:06:50 +0100
committerGitHub <noreply@github.com>2025-09-15 23:06:50 +0100
commit1c91bb9e79ef591269c8cf334cbbde86433b40c8 (patch)
tree34acab634b6053c00db0868d55ec1ede1c53c074
parent35ce82232726cb10df3cfc8a277e17e2e7960992 (diff)
parent0e801885c39b36e27c8f394c71cd93525c9f93a1 (diff)
downloadperlweeklychallenge-club-1c91bb9e79ef591269c8cf334cbbde86433b40c8.tar.gz
perlweeklychallenge-club-1c91bb9e79ef591269c8cf334cbbde86433b40c8.tar.bz2
perlweeklychallenge-club-1c91bb9e79ef591269c8cf334cbbde86433b40c8.zip
Merge pull request #12682 from pme/challenge-339
challenge-339
-rwxr-xr-xchallenge-339/peter-meszaros/perl/ch-1.pl95
-rwxr-xr-xchallenge-339/peter-meszaros/perl/ch-2.pl111
-rwxr-xr-xchallenge-339/peter-meszaros/tcl/ch-1.tcl98
-rwxr-xr-xchallenge-339/peter-meszaros/tcl/ch-2.tcl109
4 files changed, 413 insertions, 0 deletions
diff --git a/challenge-339/peter-meszaros/perl/ch-1.pl b/challenge-339/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..d61dceb6f7
--- /dev/null
+++ b/challenge-339/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Max Diff
+
+Submitted by: Mohammad Sajid Anwar
+
+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).
+
+=head2 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
+
+=head2 Example 2
+
+ Input: @ints = (1, -2, 3, -4)
+ Output: 10
+
+ Pair 1: (1, -2)
+ Pair 2: (3, -4)
+
+=head2 Example 3
+
+ Input: @ints = (-3, -1, -2, -4)
+ Output: 10
+
+ Pair 1: (-1, -2)
+ Pair 2: (-3, -4)
+
+=head2 Example 4
+
+ Input: @ints = (10, 2, 0, 5, 1)
+ Output: 50
+
+ Pair 1: (10, 5)
+ Pair 2: (0, 1)
+
+=head2 Example 5
+
+ Input: @ints = (7, 8, 9, 10, 10)
+ Output: 44
+
+ Pair 1: (10, 10)
+ Pair 2: (7, 8)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use List::Util qw/max/;
+use Algorithm::Combinatorics qw/combinations/;
+
+my $cases = [
+ [[ 5, 9, 3, 4, 6], 42, "Example 1"],
+ [[ 1, -2, 3, -4], 10, "Example 2"],
+ [[-3, -1, -2, -4], 10, "Example 3"],
+ [[10, 2, 0, 5, 1], 50, "Example 4"],
+ [[ 7, 8, 9, 10, 10], 44, "Example 5"],
+];
+
+sub max_diff
+{
+ my $ints = shift;
+
+ my $max_diff;
+ my $iter = combinations($ints, 4);
+ while (my $c = $iter->next) {
+ my $d1 = abs($c->[0] * $c->[1] - $c->[2] * $c->[3]);
+ my $d2 = abs($c->[0] * $c->[2] - $c->[1] * $c->[3]);
+ my $d3 = abs($c->[0] * $c->[3] - $c->[1] * $c->[2]);
+ my $d = max($d1, $d2, $d3);
+ $max_diff = $d if !defined($max_diff) || $d > $max_diff;
+ }
+ return $max_diff;
+}
+
+for (@$cases) {
+ is(max_diff($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-339/peter-meszaros/perl/ch-2.pl b/challenge-339/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..e41fe7dd2c
--- /dev/null
+++ b/challenge-339/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,111 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Peak Point
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of altitude gain.
+
+Write a script to find the peak point gained.
+
+=head2 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
+
+=head2 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
+
+=head2 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
+
+=head2 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
+
+=head2 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
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[ -5, 1, 5, -9, 2], 1, "Example 1"],
+ [[ 10, 10, 10, -25], 30, "Example 2"],
+ [[ 3, -4, 2, 5, -6, 1], 6, "Example 3"],
+ [[ -1, -2, -3, -4], 0, "Example 4"],
+ [[-10, 15, 5], 10, "Example 5"],
+];
+
+sub peak_point
+{
+ my $gain = shift;
+
+ my $max_altitude = 0;
+ my $current_altitude = 0;
+ for my $change (@$gain) {
+ $current_altitude += $change;
+ $max_altitude = $current_altitude if $current_altitude > $max_altitude;
+ }
+ return $max_altitude;
+}
+
+for (@$cases) {
+ is(peak_point($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-339/peter-meszaros/tcl/ch-1.tcl b/challenge-339/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..6534d4fead
--- /dev/null
+++ b/challenge-339/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,98 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Max Diff
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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)
+#
+
+package require tcltest
+package require math::combinatorics
+
+set cases {
+ {{ 5 9 3 4 6} 42 "Example 1"}
+ {{ 1 -2 3 -4} 10 "Example 2"}
+ {{-3 -1 -2 -4} 10 "Example 3"}
+ {{10 2 0 5 1} 50 "Example 4"}
+ {{ 7 8 9 10 10} 44 "Example 5"}
+}
+
+proc max_diff {ints} {
+ set len [llength $ints]
+
+ set max_diff {}
+ set comb [::math::combinatorics::combinationObj create "comb" $len 4]
+ $comb setElements $ints
+ set c [$comb nextElements]
+ while {[llength $c] == 4} {
+ set d1 [expr abs([lindex $c 0] * [lindex $c 1] - [lindex $c 2] * [lindex $c 3])]
+ set d2 [expr abs([lindex $c 0] * [lindex $c 2] - [lindex $c 1] * [lindex $c 3])]
+ set d3 [expr abs([lindex $c 0] * [lindex $c 3] - [lindex $c 1] * [lindex $c 2])]
+ set d [expr max($d1, $d2, $d3)]
+ if {![info exists max_diff] || $d > $max_diff} {
+ set max_diff $d
+ }
+ set c [$comb nextElements]
+ }
+ $comb destroy
+ return $max_diff
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ max_diff [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-339/peter-meszaros/tcl/ch-2.tcl b/challenge-339/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..a36055da71
--- /dev/null
+++ b/challenge-339/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,109 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Peak Point
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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
+#
+
+package require tcltest
+
+set cases {
+ {{ -5 1 5 -9 2} 1 "Example 1"}
+ {{ 10 10 10 -25} 30 "Example 2"}
+ {{ 3 -4 2 5 -6 1} 6 "Example 3"}
+ {{ -1 -2 -3 -4} 0 "Example 4"}
+ {{-10 15 5} 10 "Example 5"}
+}
+
+proc peak_point {gain} {
+ set max_altitude 0
+ set current_altitude 0
+ foreach change $gain {
+ set current_altitude [expr $current_altitude + $change]
+ if {$current_altitude > $max_altitude} {
+ set max_altitude $current_altitude
+ }
+ }
+ return $max_altitude
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ peak_point [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+