aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-08 20:04:29 +0100
committerGitHub <noreply@github.com>2025-09-08 20:04:29 +0100
commitab5027b2cef75a7f70814d2d31575102688d35b7 (patch)
treeea760399a1b6e0b933551cc87b20b38864dc306b
parent87708a66a388c7a2fe34638509484dbfb0c24b61 (diff)
parentb5be70aa9197461f88d8d65e12f3a589980335d9 (diff)
downloadperlweeklychallenge-club-ab5027b2cef75a7f70814d2d31575102688d35b7.tar.gz
perlweeklychallenge-club-ab5027b2cef75a7f70814d2d31575102688d35b7.tar.bz2
perlweeklychallenge-club-ab5027b2cef75a7f70814d2d31575102688d35b7.zip
Merge pull request #12650 from pme/challenge-338
challenge-338
-rwxr-xr-xchallenge-338/peter-meszaros/perl/ch-1.pl93
-rwxr-xr-xchallenge-338/peter-meszaros/perl/ch-2.pl210
-rwxr-xr-xchallenge-338/peter-meszaros/tcl/ch-1.tcl93
-rwxr-xr-xchallenge-338/peter-meszaros/tcl/ch-2.tcl210
4 files changed, 606 insertions, 0 deletions
diff --git a/challenge-338/peter-meszaros/perl/ch-1.pl b/challenge-338/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..97ba7feccf
--- /dev/null
+++ b/challenge-338/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Highest Row
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a m x n matrix.
+
+Write a script to find the highest row sum in the given matrix.
+
+=head2 Example 1
+
+ Input: @matrix = ([4, 4, 4, 4],
+ [10, 0, 0, 0],
+ [2, 2, 2, 9])
+ Output: 16
+
+ Row 1: 4 + 4 + 4 + 4 => 16
+ Row 2: 10 + 0 + 0 + 0 => 10
+ Row 3: 2 + 2 + 2 + 9 => 15
+
+=head2 Example 2
+
+ Input: @matrix = ([1, 5],
+ [7, 3],
+ [3, 5])
+ Output: 10
+
+=head2 Example 3
+
+ Input: @matrix = ([1, 2, 3],
+ [3, 2, 1])
+ Output: 6
+
+=head2 Example 4
+
+ Input: @matrix = ([2, 8, 7],
+ [7, 1, 3],
+ [1, 9, 5])
+ Output: 17
+
+=head2 Example 5
+
+ Input: @matrix = ([10, 20, 30],
+ [5, 5, 5],
+ [0, 100, 0],
+ [25, 25, 25])
+ Output: 100
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[[4, 4, 4, 4],
+ [10, 0, 0, 0],
+ [2, 2, 2, 9]], 16, "Example 1"],
+ [[[1, 5],
+ [7, 3],
+ [3, 5]], 10, "Example 2"],
+ [[[1, 2, 3],
+ [3, 2, 1]], 6, "Example 3"],
+ [[[2, 8, 7],
+ [7, 1, 3],
+ [1, 9, 5]], 17, "Example 4"],
+ [[[10, 20, 30],
+ [5, 5, 5],
+ [0, 100, 0],
+ [25, 25, 25]], 100, "Example 5"],
+];
+
+sub highest_row
+{
+ my $matrix = shift;
+
+ my $max = 0;
+ for my $row (@$matrix) {
+ my $sum = 0;
+ $sum += $row->[$_] for 0 .. $#$row;
+ $max = $sum if $sum > $max;
+ }
+ return $max;
+}
+
+for (@$cases) {
+ is(highest_row($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-338/peter-meszaros/perl/ch-2.pl b/challenge-338/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..eef2692628
--- /dev/null
+++ b/challenge-338/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,210 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Max Distance
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given two integer arrays, @arr1 and @arr2.
+
+Write a script to find the maximum difference between any pair of values from
+both arrays.
+
+=head2 Example 1
+
+ Input: @arr1 = (4, 5, 7)
+ @arr2 = (9, 1, 3, 4)
+ Output: 6
+
+ With element $arr1[0] = 4
+ | 4 - 9 | = 5
+ | 4 - 1 | = 3
+ | 4 - 3 | = 1
+ | 4 - 4 | = 0
+ max distance = 5
+
+ With element $arr1[1] = 5
+ | 5 - 9 | = 4
+ | 5 - 1 | = 4
+ | 5 - 3 | = 2
+ | 5 - 4 | = 1
+ max distance = 4
+
+ With element $arr1[2] = 7
+ | 7 - 9 | = 2
+ | 7 - 1 | = 6
+ | 7 - 3 | = 4
+ | 7 - 4 | = 4
+ max distance = 6
+
+ max (5, 6, 6) = 6
+
+=head2 Example 2
+
+ Input: @arr1 = (2, 3, 5, 4)
+ @arr2 = (3, 2, 5, 5, 8, 7)
+ Output: 6
+
+ With element $arr1[0] = 2
+ | 2 - 3 | = 1
+ | 2 - 2 | = 0
+ | 2 - 5 | = 3
+ | 2 - 5 | = 3
+ | 2 - 8 | = 6
+ | 2 - 7 | = 5
+ max distance = 6
+
+ With element $arr1[1] = 3
+ | 3 - 3 | = 0
+ | 3 - 2 | = 1
+ | 3 - 5 | = 2
+ | 3 - 5 | = 2
+ | 3 - 8 | = 5
+ | 3 - 7 | = 4
+ max distance = 5
+
+ With element $arr1[2] = 5
+ | 5 - 3 | = 2
+ | 5 - 2 | = 3
+ | 5 - 5 | = 0
+ | 5 - 5 | = 0
+ | 5 - 8 | = 3
+ | 5 - 7 | = 2
+ max distance = 3
+
+ With element $arr1[3] = 4
+ | 4 - 3 | = 1
+ | 4 - 2 | = 2
+ | 4 - 5 | = 1
+ | 4 - 5 | = 1
+ | 4 - 8 | = 4
+ | 4 - 7 | = 3
+ max distance = 4
+
+ max (5, 6, 3, 4) = 6
+
+=head2 Example 3
+
+ Input: @arr1 = (2, 1, 11, 3)
+ @arr2 = (2, 5, 10, 2)
+ Output: 9
+
+ With element $arr1[0] = 2
+ | 2 - 2 | = 0
+ | 2 - 5 | = 3
+ | 2 - 10 | = 8
+ | 2 - 2 | = 0
+ max distance = 8
+
+ With element $arr1[1] = 1
+ | 1 - 2 | = 1
+ | 1 - 5 | = 4
+ | 1 - 10 | = 9
+ | 1 - 2 | = 1
+ max distance = 9
+
+ With element $arr1[2] = 11
+ | 11 - 2 | = 9
+ | 11 - 5 | = 6
+ | 11 - 10 | = 1
+ | 11 - 2 | = 9
+ max distance = 9
+
+ With element $arr1[3] = 3
+ | 3 - 2 | = 1
+ | 3 - 5 | = 2
+ | 3 - 10 | = 7
+ | 3 - 2 | = 1
+ max distance = 7
+
+ max (8, 9, 9, 7) = 9
+
+=head2 Example 4
+
+ Input: @arr1 = (1, 2, 3)
+ @arr2 = (3, 2, 1)
+ Output: 2
+
+ With element $arr1[0] = 1
+ | 1 - 3 | = 2
+ | 1 - 2 | = 1
+ | 1 - 1 | = 0
+ max distance = 2
+
+ With element $arr1[1] = 2
+ | 2 - 3 | = 1
+ | 2 - 2 | = 0
+ | 2 - 1 | = 1
+ max distance = 1
+
+ With element $arr1[2] = 3
+ | 3 - 3 | = 0
+ | 3 - 2 | = 1
+ | 3 - 1 | = 2
+ max distance = 2
+
+ max (2, 1, 2) = 2
+
+=head2 Example 5
+
+ Input: @arr1 = (1, 0, 2, 3)
+ @arr2 = (5, 0)
+ Output: 5
+
+ With element $arr1[0] = 1
+ | 1 - 5 | = 4
+ | 1 - 0 | = 1
+ max distance = 4
+
+ With element $arr1[1] = 0
+ | 0 - 5 | = 5
+ | 0 - 0 | = 0
+ max distance = 5
+
+ With element $arr1[2] = 2
+ | 2 - 5 | = 3
+ | 2 - 0 | = 2
+ max distance = 3
+
+ With element $arr1[3] = 3
+ | 3 - 5 | = 2
+ | 3 - 0 | = 3
+ max distance = 3
+
+ max (4, 5, 3, 3) = 5
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[[4, 5, 7], [9, 1, 3, 4]], 6, "Example 1"],
+ [[[2, 3, 5, 4], [3, 2, 5, 5, 8, 7]], 6, "Example 2"],
+ [[[2, 1, 11, 3], [2, 5, 10, 2]], 9, "Example 3"],
+ [[[1, 2, 3], [3, 2, 1]], 2, "Example 4"],
+ [[[1, 0, 2, 3], [5, 0]], 5, "Example 5"],
+];
+
+sub max_distance
+{
+ my $arr1 = $_[0]->[0];
+ my $arr2 = $_[0]->[1];
+
+ my $max = 0;
+ for my $i (0 .. $#$arr1) {
+ for my $j (0 .. $#$arr2) {
+ my $dist = abs($arr1->[$i] - $arr2->[$j]);
+ $max = $dist if $dist > $max;
+ }
+ }
+ return $max;
+}
+
+for (@$cases) {
+ is(max_distance($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-338/peter-meszaros/tcl/ch-1.tcl b/challenge-338/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..abd09b3403
--- /dev/null
+++ b/challenge-338/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,93 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Highest Row
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a m x n matrix.
+#
+# Write a script to find the highest row sum in the given matrix.
+#
+# Example 1
+#
+# Input: @matrix = ([4, 4, 4, 4],
+# [10, 0, 0, 0],
+# [2, 2, 2, 9])
+# Output: 16
+#
+# Row 1: 4 + 4 + 4 + 4 => 16
+# Row 2: 10 + 0 + 0 + 0 => 10
+# Row 3: 2 + 2 + 2 + 9 => 15
+#
+# Example 2
+#
+# Input: @matrix = ([1, 5],
+# [7, 3],
+# [3, 5])
+# Output: 10
+#
+# Example 3
+#
+# Input: @matrix = ([1, 2, 3],
+# [3, 2, 1])
+# Output: 6
+#
+# Example 4
+#
+# Input: @matrix = ([2, 8, 7],
+# [7, 1, 3],
+# [1, 9, 5])
+# Output: 17
+#
+# Example 5
+#
+# Input: @matrix = ([10, 20, 30],
+# [5, 5, 5],
+# [0, 100, 0],
+# [25, 25, 25])
+# Output: 100
+#
+
+package require tcltest
+
+set cases {
+ {{{ 4 4 4 4}
+ {10 0 0 0}
+ { 2 2 2 9}} 16 "Example 1"}
+ {{{1 5}
+ {7 3}
+ {3 5}} 10 "Example 2"}
+ {{{1 2 3}
+ {3 2 1}} 6 "Example 3"}
+ {{{2 8 7}
+ {7 1 3}
+ {1 9 5}} 17 "Example 4"}
+ {{{10 20 30}
+ { 5 5 5}
+ { 0 100 0}
+ {25 25 25}} 100 "Example 5"}
+}
+
+proc highest_row {m} {
+ set max 0
+ foreach row $m {
+ set sum 0
+ foreach val $row {
+ set sum [expr $sum + $val]
+ }
+ if {$sum > $max} {
+ set max $sum
+ }
+ }
+ return $max
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ highest_row [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-338/peter-meszaros/tcl/ch-2.tcl b/challenge-338/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..b2c26322bd
--- /dev/null
+++ b/challenge-338/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,210 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Max Distance
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given two integer arrays, @arr1 and @arr2.
+#
+# Write a script to find the maximum difference between any pair of values from
+# both arrays.
+#
+# Example 1
+#
+# Input: @arr1 = (4, 5, 7)
+# @arr2 = (9, 1, 3, 4)
+# Output: 6
+#
+# With element $arr1[0] = 4
+# | 4 - 9 | = 5
+# | 4 - 1 | = 3
+# | 4 - 3 | = 1
+# | 4 - 4 | = 0
+# max distance = 5
+#
+# With element $arr1[1] = 5
+# | 5 - 9 | = 4
+# | 5 - 1 | = 4
+# | 5 - 3 | = 2
+# | 5 - 4 | = 1
+# max distance = 4
+#
+# With element $arr1[2] = 7
+# | 7 - 9 | = 2
+# | 7 - 1 | = 6
+# | 7 - 3 | = 4
+# | 7 - 4 | = 4
+# max distance = 6
+#
+# max (5, 6, 6) = 6
+#
+# Example 2
+#
+# Input: @arr1 = (2, 3, 5, 4)
+# @arr2 = (3, 2, 5, 5, 8, 7)
+# Output: 6
+#
+# With element $arr1[0] = 2
+# | 2 - 3 | = 1
+# | 2 - 2 | = 0
+# | 2 - 5 | = 3
+# | 2 - 5 | = 3
+# | 2 - 8 | = 6
+# | 2 - 7 | = 5
+# max distance = 6
+#
+# With element $arr1[1] = 3
+# | 3 - 3 | = 0
+# | 3 - 2 | = 1
+# | 3 - 5 | = 2
+# | 3 - 5 | = 2
+# | 3 - 8 | = 5
+# | 3 - 7 | = 4
+# max distance = 5
+#
+# With element $arr1[2] = 5
+# | 5 - 3 | = 2
+# | 5 - 2 | = 3
+# | 5 - 5 | = 0
+# | 5 - 5 | = 0
+# | 5 - 8 | = 3
+# | 5 - 7 | = 2
+# max distance = 3
+#
+# With element $arr1[3] = 4
+# | 4 - 3 | = 1
+# | 4 - 2 | = 2
+# | 4 - 5 | = 1
+# | 4 - 5 | = 1
+# | 4 - 8 | = 4
+# | 4 - 7 | = 3
+# max distance = 4
+#
+# max (5, 6, 3, 4) = 6
+#
+# Example 3
+#
+# Input: @arr1 = (2, 1, 11, 3)
+# @arr2 = (2, 5, 10, 2)
+# Output: 9
+#
+# With element $arr1[0] = 2
+# | 2 - 2 | = 0
+# | 2 - 5 | = 3
+# | 2 - 10 | = 8
+# | 2 - 2 | = 0
+# max distance = 8
+#
+# With element $arr1[1] = 1
+# | 1 - 2 | = 1
+# | 1 - 5 | = 4
+# | 1 - 10 | = 9
+# | 1 - 2 | = 1
+# max distance = 9
+#
+# With element $arr1[2] = 11
+# | 11 - 2 | = 9
+# | 11 - 5 | = 6
+# | 11 - 10 | = 1
+# | 11 - 2 | = 9
+# max distance = 9
+#
+# With element $arr1[3] = 3
+# | 3 - 2 | = 1
+# | 3 - 5 | = 2
+# | 3 - 10 | = 7
+# | 3 - 2 | = 1
+# max distance = 7
+#
+# max (8, 9, 9, 7) = 9
+#
+# Example 4
+#
+# Input: @arr1 = (1, 2, 3)
+# @arr2 = (3, 2, 1)
+# Output: 2
+#
+# With element $arr1[0] = 1
+# | 1 - 3 | = 2
+# | 1 - 2 | = 1
+# | 1 - 1 | = 0
+# max distance = 2
+#
+# With element $arr1[1] = 2
+# | 2 - 3 | = 1
+# | 2 - 2 | = 0
+# | 2 - 1 | = 1
+# max distance = 1
+#
+# With element $arr1[2] = 3
+# | 3 - 3 | = 0
+# | 3 - 2 | = 1
+# | 3 - 1 | = 2
+# max distance = 2
+#
+# max (2, 1, 2) = 2
+#
+# Example 5
+#
+# Input: @arr1 = (1, 0, 2, 3)
+# @arr2 = (5, 0)
+# Output: 5
+#
+# With element $arr1[0] = 1
+# | 1 - 5 | = 4
+# | 1 - 0 | = 1
+# max distance = 4
+#
+# With element $arr1[1] = 0
+# | 0 - 5 | = 5
+# | 0 - 0 | = 0
+# max distance = 5
+#
+# With element $arr1[2] = 2
+# | 2 - 5 | = 3
+# | 2 - 0 | = 2
+# max distance = 3
+#
+# With element $arr1[3] = 3
+# | 3 - 5 | = 2
+# | 3 - 0 | = 3
+# max distance = 3
+#
+# max (4, 5, 3, 3) = 5
+#
+
+package require tcltest
+
+set cases {
+ {{{4 5 7} {9 1 3 4}} 6 "Example 1"}
+ {{{2 3 5 4} {3 2 5 5 8 7}} 6 "Example 2"}
+ {{{2 1 11 3} {2 5 10 2}} 9 "Example 3"}
+ {{{1 2 3} {3 2 1}} 2 "Example 4"}
+ {{{1 0 2 3} {5 0}} 5 "Example 5"}
+}
+
+proc max_distance {p} {
+ set arr1 [lindex $p 0]
+ set arr2 [lindex $p 1]
+
+ set max 0
+ foreach i $arr1 {
+ foreach j $arr2 {
+ set dist [expr abs($i - $j)]
+ if {$dist > $max} {
+ set max $dist
+ }
+ }
+ }
+ return $max
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ max_distance [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+