Perl Weekly Challenge 338.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 338.

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

This task is very simple using the Perl Data Language PDL which has methods sumover to sum rows of a matrix, or more generally, of an ndarray, and max to get the maximum value of the resulting array. The code fits a half-liner.

Examples:

perl -MPDL -E '
for(@ARGV){say "$_ -> ", pdl($_)->sumover->max}
' "[ [4 4 4 4][10 0 0 0][2 2 2 9]]" "[ [1 5][7 3][3 5]]" "[ [1 2 3][3 2 1]]" \
  "[ [2 8 7][7 1 3][1 9 5]]" "[ [10 20  30][5 5 5][0 100 0][25, 25,  25]]"

Results:

[ [4 4 4 4][10 0 0 0][2 2 2 9]] -> 16
[ [1 5][7 3][3 5]] -> 10
[ [1 2 3][3 2 1]] -> 6
[ [2 8 7][7 1 3][1 9 5]] -> 17
[ [10 20  30][5 5 5][0 100 0][25, 25,  25]] -> 100

The full code is similar:

 1  # Perl weekly challenge 338
 2  # Task 1:  Highest Row
 3  #
 4  # See https://wlmb.github.io/2025/09/07/PWC338/#task-1-highest-row
 5  use v5.36;
 6  use feature qw(try);
 7  use PDL;
 8  die <<~"FIN" unless @ARGV;
 9      Usage: $0 M1 M2...
10      to find the maximum rowwise sum of the elements of the
11      matrices Mi. The inputs are strings of the form
12      "[[m00 m01...][m10 m11...]...]" which may be parsed
13      by pdl as a matrix
14      FIN
15  for(@ARGV){
16      try{say "$_ -> ", pdl($_)->sumover->max}
17      catch($e){warn $e}
18  }

I use try/catch in case there are input errors.

Examples:

./ch-1.pl "[ [4 4 4 4][10 0 0 0][2 2 2 9]]" "[ [1 5][7 3][3 5]]" "[ [1 2 3][3 2 1]]" \
          "[ [2 8 7][7 1 3][1 9 5]]" "[ [10 20  30][5 5 5][0 100 0][25, 25,  25]]"

Results:

[ [4 4 4 4][10 0 0 0][2 2 2 9]] -> 16
[ [1 5][7 3][3 5]] -> 10
[ [1 2 3][3 2 1]] -> 6
[ [2 8 7][7 1 3][1 9 5]] -> 17
[ [10 20  30][5 5 5][0 100 0][25, 25,  25]] -> 100

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

This is another problem that can be very simply solved with PDL, as it can convert 1D arrays to 2D matrices by adding dummy dimensions, i.e., repeating its columns or its rows, and adding dummy trailing dimensions can be done intrinsically. Then I can subtract both matrices, take the absolute value and search for the maximum . The result takes a one-liner.

Examples:

perl -MPDL -E '
for my ($x, $y)(@ARGV){say "$x, $y -> ", (pdl($x)-pdl($y)->dummy(0))->abs->max}
' "[4 5 7]" "[9 1 3 4]" "[2 3 5 4]" "[3 2 5 5 8 7]" "[2 1 11 3]" "[2 5 10 2]" \
  "[1 2 3]" "[3 2 1]" "[1 0 2 3]" "[5 0]"

Results:

[4 5 7], [9 1 3 4] -> 6
[2 3 5 4], [3 2 5 5 8 7] -> 6
[2 1 11 3], [2 5 10 2] -> 9
[1 2 3], [3 2 1] -> 2
[1 0 2 3], [5 0] -> 5

This solution makes many unneeded subtractions. It would be enough to subtract the minima and maxima from both arrays. This yields a slightly longer but more efficient solution (for large arrays).

Examples:

perl -MPDL -MPDL::NiceSlice -E '
for my ($x, $y)(@ARGV){($p, $q)=map pdl(pdl($_)->minmax),($x,$y);say "$x, $y -> ",
($p-$q(-1:0))->abs->max}
' "[4 5 7]" "[9 1 3 4]" "[2 3 5 4]" "[3 2 5 5 8 7]" "[2 1 11 3]" "[2 5 10 2]" \
  "[1 2 3]" "[3 2 1]" "[1 0 2 3]" "[5 0]"

Results:

[4 5 7], [9 1 3 4] -> 6
[2 3 5 4], [3 2 5 5 8 7] -> 6
[2 1 11 3], [2 5 10 2] -> 9
[1 2 3], [3 2 1] -> 2
[1 0 2 3], [5 0] -> 5

The full code corresponding to the last solution is:

 1  # Perl weekly challenge 338
 2  # Task 2:  Max Distance
 3  #
 4  # See https://wlmb.github.io/2025/09/07/PWC338/#task-2-max-distance
 5  use v5.36;
 6  use feature qw(try);
 7  use PDL;
 8  use PDL::NiceSlice;
 9  die <<~"FIN" unless @ARGV and @ARGV%2==0;
10      Usage: $0 X1 Y1 X2 Y2...
11      to find the maximum distance between the elements of array Xi and Yi.
12      The inputs Xi and Yi are strings that may be parsed by ~PDL~ as arrays
13      of the form "[z0 z1...]"
14      FIN
15  for my ($x, $y)(@ARGV){
16      try {
17          # initialize $p and $q with the min and max of each input
18          my ($p, $q)=map {pdl(pdl($_)->minmax)} ($x,$y);
19          # reverse one array to subtract minimum from maximum and maximum from minimum
20          my $result = ($p-$q(-1:0))->abs->max;
21          say "$x, $y -> ", $result;
22      }
23      catch($e){ warn $e }
24  }

Example:

./ch-2.pl "[4 5 7]" "[9 1 3 4]" "[2 3 5 4]" "[3 2 5 5 8 7]" "[2 1 11 3]" "[2 5 10 2]" \
          "[1 2 3]" "[3 2 1]" "[1 0 2 3]" "[5 0]"

Results:

[4 5 7], [9 1 3 4] -> 6
[2 3 5 4], [3 2 5 5 8 7] -> 6
[2 1 11 3], [2 5 10 2] -> 9
[1 2 3], [3 2 1] -> 2
[1 0 2 3], [5 0] -> 5

/;

Written on September 7, 2025