From 9cb820a72cb95a2e4f1c3f0caed83aaa66db0abb Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 9 Sep 2025 13:35:28 +0200 Subject: Add solutions for The Weekly Challenge - 338 - Implemented Task 1 (Highest Row) in Perl (ch-1.pl) and Python (ch-1.py) to find the maximum row sum in a matrix. - Implemented Task 2 (Max Distance) in Perl (ch-2.pl) and Python (ch-2.py) to find the maximum absolute difference between pairs from two arrays. - Included docstrings, type annotations (Python), and provided unit tests for both tasks. - Added README.md summarizing tasks and solutions. - Verified all unit tests pass for both Perl and Python solutions. --- challenge-338/lubos-kolouch/README | 75 ++++++++++++++++++++++++++++++ challenge-338/lubos-kolouch/perl/ch-1.pl | 57 +++++++++++++++++++++++ challenge-338/lubos-kolouch/perl/ch-2.pl | 60 ++++++++++++++++++++++++ challenge-338/lubos-kolouch/python/ch-1.py | 47 +++++++++++++++++++ challenge-338/lubos-kolouch/python/ch-2.py | 45 ++++++++++++++++++ 5 files changed, 284 insertions(+) create mode 100644 challenge-338/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-338/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-338/lubos-kolouch/python/ch-1.py create mode 100644 challenge-338/lubos-kolouch/python/ch-2.py diff --git a/challenge-338/lubos-kolouch/README b/challenge-338/lubos-kolouch/README index 921b2d9f4a..b3e885f6ab 100644 --- a/challenge-338/lubos-kolouch/README +++ b/challenge-338/lubos-kolouch/README @@ -1 +1,76 @@ Solutions by Lubos Kolouch. + +# The Weekly Challenge - 338 + +## Task 1: Highest Row + +### Description + +Given an m x n matrix, the task is to find the highest row sum in the matrix. Each row is summed, and the maximum sum is returned. + +### Solution + +#### Perl (ch-1.pl) + +The Perl solution defines a function `highest_row` that takes a reference to a 2D array (matrix). It iterates through each row, computes the sum of the elements, and tracks the maximum sum. Input validation ensures the matrix is not empty. + +- **Input**: A 2D array reference (`@matrix`). +- **Output**: The maximum sum of any row. +- **Approach**: Use a loop to sum each row and update the maximum sum if the current row's sum is larger. + +#### Python (ch-1.py) + +The Python solution defines a function `highest_row` that takes a 2D list (matrix). It uses Python's `sum` function to compute each row's sum and tracks the maximum using the `max` function. Type hints are included for clarity, and input validation checks for empty matrices. + +- **Input**: A 2D list (`matrix: List[List[int]]`). +- **Output**: An integer representing the maximum row sum. +- **Approach**: Iterate through rows, compute sums, and track the maximum. + +### Examples + +- Example 1: `[[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]` → `16` +- Example 2: `[[1, 5], [7, 3], [3, 5]]` → `10` +- Example 3: `[[1, 2, 3], [3, 2, 1]]` → `6` +- Example 4: `[[2, 8, 7], [7, 1, 3], [1, 9, 5]]` → `17` +- Example 5: `[[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]]` → `100` + +## Task 2: Max Distance + +### Description + +Given two integer arrays, the task is to find the maximum absolute difference between any pair of values from the two arrays. + +### Solution + +#### Perl (ch-2.pl) + +The Perl solution defines a function `max_distance` that takes references to two arrays. It computes the absolute difference between each pair of elements from the two arrays using nested loops and tracks the maximum difference. Input validation ensures both arrays are non-empty. + +- **Input**: Two array references (`@arr1`, `@arr2`). +- **Output**: The maximum absolute difference between any pair of elements. +- **Approach**: Use nested loops to compute `abs(x - y)` for each pair and update the maximum. + +#### Python (ch-2.py) + +The Python solution defines a function `max_distance` that takes two lists of integers. It uses nested loops to compute the absolute difference between each pair of elements and tracks the maximum using the `max` function. Type hints are included, and input validation checks for empty arrays. + +- **Input**: Two lists (`arr1: List[int]`, `arr2: List[int]`). +- **Output**: An integer representing the maximum absolute difference. +- **Approach**: Iterate through all pairs, compute absolute differences, and track the maximum. + +### Examples + +- Example 1: `arr1 = [4, 5, 7], arr2 = [9, 1, 3, 4]` → `6` +- Example 2: `arr1 = [2, 3, 5, 4], arr2 = [3, 2, 5, 5, 8, 7]` → `6` +- Example 3: `arr1 = [2, 1, 11, 3], arr2 = [2, 5, 10, 2]` → `9` +- Example 4: `arr1 = [1, 2, 3], arr2 = [3, 2, 1]` → `2` +- Example 5: `arr1 = [1, 0, 2, 3], arr2 = [5, 0]` → `5` + +## Running the Code + +### Perl + +Run the Perl scripts with: +```bash +perl ch-1.pl +perl ch-2.pl diff --git a/challenge-338/lubos-kolouch/perl/ch-1.pl b/challenge-338/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..40cf8ee184 --- /dev/null +++ b/challenge-338/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +=pod + +=head1 NAME + +ch-1.pl - Highest Row + +=head1 DESCRIPTION + +Given an m x n matrix, this script finds the highest row sum in the matrix. + +=head1 METHODS + +=over 4 + +=item highest_row(\@matrix) + +Takes a reference to a 2D array (matrix) and returns the maximum sum of any row. + +=back + +=head1 EXAMPLES + +See the test cases below for examples. + +=cut + +sub highest_row { + my ($matrix) = @_; + + # Input validation + return 0 unless defined $matrix && @$matrix > 0; + + my $max_sum = 0; + + # Calculate sum for each row and track maximum + foreach my $row (@$matrix) { + my $row_sum = 0; + $row_sum += $_ for @$row; + $max_sum = $row_sum if $row_sum > $max_sum; + } + + return $max_sum; +} + +# Unit tests +is( highest_row( [ [ 4, 4, 4, 4 ], [ 10, 0, 0, 0 ], [ 2, 2, 2, 9 ] ] ), 16, 'Example 1' ); +is( highest_row( [ [ 1, 5 ], [ 7, 3 ], [ 3, 5 ] ] ), 10, 'Example 2' ); +is( highest_row( [ [ 1, 2, 3 ], [ 3, 2, 1 ] ] ), 6, 'Example 3' ); +is( highest_row( [ [ 2, 8, 7 ], [ 7, 1, 3 ], [ 1, 9, 5 ] ] ), 17, 'Example 4' ); +is( highest_row( [ [ 10, 20, 30 ], [ 5, 5, 5 ], [ 0, 100, 0 ], [ 25, 25, 25 ] ] ), 100, 'Example 5' ); + +done_testing(); diff --git a/challenge-338/lubos-kolouch/perl/ch-2.pl b/challenge-338/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..c1f4e1ecba --- /dev/null +++ b/challenge-338/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +=pod + +=head1 NAME + +ch-2.pl - Max Distance + +=head1 DESCRIPTION + +Given two integer arrays, this script finds the maximum difference between any pair of values +from both arrays. + +=head1 METHODS + +=over 4 + +=item max_distance(\@arr1, \@arr2) + +Takes references to two arrays and returns the maximum absolute difference between any pair +of values from the two arrays. + +=back + +=head1 EXAMPLES + +See the test cases below for examples. + +=cut + +sub max_distance { + my ( $arr1, $arr2 ) = @_; + + # Input validation + return 0 unless defined $arr1 && defined $arr2 && @$arr1 > 0 && @$arr2 > 0; + + my $max_diff = 0; + + # Calculate absolute difference for each pair and track maximum + foreach my $x (@$arr1) { + foreach my $y (@$arr2) { + my $diff = abs( $x - $y ); + $max_diff = $diff if $diff > $max_diff; + } + } + + return $max_diff; +} + +# Unit tests +is( max_distance( [ 4, 5, 7 ], [ 9, 1, 3, 4 ] ), 6, 'Example 1' ); +is( max_distance( [ 2, 3, 5, 4 ], [ 3, 2, 5, 5, 8, 7 ] ), 6, 'Example 2' ); +is( max_distance( [ 2, 1, 11, 3 ], [ 2, 5, 10, 2 ] ), 9, 'Example 3' ); +is( max_distance( [ 1, 2, 3 ], [ 3, 2, 1 ] ), 2, 'Example 4' ); +is( max_distance( [ 1, 0, 2, 3 ], [ 5, 0 ] ), 5, 'Example 5' ); + +done_testing(); diff --git a/challenge-338/lubos-kolouch/python/ch-1.py b/challenge-338/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..0917bd7ef1 --- /dev/null +++ b/challenge-338/lubos-kolouch/python/ch-1.py @@ -0,0 +1,47 @@ +def highest_row(matrix: list[list[int]]) -> int: + """ + Find the highest row sum in a given m x n matrix. + + Args: + matrix (List[List[int]]): A 2D list representing the matrix. + + Returns: + int: The maximum sum of any row in the matrix. + """ + if not matrix or not matrix[0]: + return 0 + + max_sum = 0 + for row in matrix: + row_sum = sum(row) + max_sum = max(max_sum, row_sum) + + return max_sum + + +# Unit tests +if __name__ == '__main__': + import unittest + + class TestHighestRow(unittest.TestCase): + + def test_example1(self): + self.assertEqual( + highest_row([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]), 16) + + def test_example2(self): + self.assertEqual(highest_row([[1, 5], [7, 3], [3, 5]]), 10) + + def test_example3(self): + self.assertEqual(highest_row([[1, 2, 3], [3, 2, 1]]), 6) + + def test_example4(self): + self.assertEqual(highest_row([[2, 8, 7], [7, 1, 3], [1, 9, 5]]), + 17) + + def test_example5(self): + self.assertEqual( + highest_row([[10, 20, 30], [5, 5, 5], [0, 100, 0], + [25, 25, 25]]), 100) + + unittest.main(argv=['first-arg-is-ignored'], exit=False) diff --git a/challenge-338/lubos-kolouch/python/ch-2.py b/challenge-338/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..7e19bd31ff --- /dev/null +++ b/challenge-338/lubos-kolouch/python/ch-2.py @@ -0,0 +1,45 @@ +def max_distance(arr1: list[int], arr2: list[int]) -> int: + """ + Find the maximum difference between any pair of values from two integer arrays. + + Args: + arr1 (List[int]): First integer array. + arr2 (List[int]): Second integer array. + + Returns: + int: The maximum absolute difference between any pair of values from the two arrays. + """ + if not arr1 or not arr2: + return 0 + + max_diff = 0 + for x in arr1: + for y in arr2: + diff = abs(x - y) + max_diff = max(max_diff, diff) + + return max_diff + + +# Unit tests +if __name__ == '__main__': + import unittest + + class TestMaxDistance(unittest.TestCase): + + def test_example1(self): + self.assertEqual(max_distance([4, 5, 7], [9, 1, 3, 4]), 6) + + def test_example2(self): + self.assertEqual(max_distance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]), 6) + + def test_example3(self): + self.assertEqual(max_distance([2, 1, 11, 3], [2, 5, 10, 2]), 9) + + def test_example4(self): + self.assertEqual(max_distance([1, 2, 3], [3, 2, 1]), 2) + + def test_example5(self): + self.assertEqual(max_distance([1, 0, 2, 3], [5, 0]), 5) + + unittest.main(argv=['first-arg-is-ignored'], exit=False) -- cgit