Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #338 (Theme: “Mad Max Beyond Perldome”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-09-08 through 2025-09-14 is #338

The tasks for challenge #338 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 338-1: Highest Row
Submitted by: Mohammad Sajid Anwar
You are given a m x n matrix of real numbers. Write a script to
find the highest row sum in the given matrix.
(
   # Example #1 input:
   [ [4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9] ],
   # Expected output: 16

   # Example #2 input:
   [ [1, 5], [7, 3], [3, 5] ],
   # Expected output: 10

   # Example #3 input:
   [ [1, 2, 3], [3, 2, 1] ],
   # Expected output: 6

   # Example #4 input:
   [ [2, 8, 7], [7, 1, 3], [1, 9, 5] ],
   # Expected output: 17

   # Example #5 input:
   [ [10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25] ],
   # Expected output: 100
);

I'll start by setting a "$max" variable to POSIX "-Inf", then I'll use "sum0" from List::Util to sum each row, and if any row sum is greater than $max I'll set $max to that sum. I'll then return $max.

Robbie Hatley's Perl Solution to The Weekly Challenge 338-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 338-2: Max Distance
Submitted by: Mohammad Sajid Anwar
You are given two arrays of real numbers. Write a script to find
the maximum distance between any pair of values from both arrays.
(
   # Example #1 input:
   [[4, 5, 7], [9, 1, 3, 4]],
   # Expected output: 6

   # Example #2 input:
   [[2, 3, 5, 4], [3, 2, 5, 5, 8, 7]],
   # Expected output: 6

   # Example #3 input:
   [[2, 1, 11, 3], [2, 5, 10, 2]],
   # Expected output: 9

   # Example #4 input:
   [[1, 2, 3], [3, 2, 1]],
   # Expected output: 2

   # Example 5 input:
   [[1, 0, 2, 3], [5, 0]],
   # Expected output: 5
);

Since the title says "distance", not "difference", I'll start by setting a "$max" variable to 0. Then I'll use a pair of nested for loops over the two arrays to calculate the distance between each pair, and if any distance is greater than $max, I'll set $max to that distance. I'll then return $max.

Robbie Hatley's Perl Solution to The Weekly Challenge 338-2

That's it for challenge 338; see you on challenge 339!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)