diff options
| author | Simon Green <mail@simon.green> | 2025-09-14 22:54:18 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-09-14 22:54:18 +1000 |
| commit | 045da04e63323acdfffc3d1a0cd92bf0853364b0 (patch) | |
| tree | 643f80cc8f66f05500e94db273ea9faf700636b8 | |
| parent | 12d9b7370121f12b601562856772ed3c03f7bb94 (diff) | |
| download | perlweeklychallenge-club-045da04e63323acdfffc3d1a0cd92bf0853364b0.tar.gz perlweeklychallenge-club-045da04e63323acdfffc3d1a0cd92bf0853364b0.tar.bz2 perlweeklychallenge-club-045da04e63323acdfffc3d1a0cd92bf0853364b0.zip | |
sgreen solutions to challenge 338
| -rw-r--r-- | challenge-338/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-338/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-338/sgreen/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-338/sgreen/perl/ch-2.pl | 22 | ||||
| -rwxr-xr-x | challenge-338/sgreen/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-338/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-338/sgreen/python/test.py | 41 |
7 files changed, 138 insertions, 2 deletions
diff --git a/challenge-338/sgreen/README.md b/challenge-338/sgreen/README.md index a207f68abf..65d76b613e 100644 --- a/challenge-338/sgreen/README.md +++ b/challenge-338/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 337 +# The Weekly Challenge 338 -Blog: [Oddly small](https://dev.to/simongreennet/weekly-challenge-oddly-small-1g9o) +Blog: [The Highest Distance](https://dev.to/simongreennet/weekly-challenge-the-highest-distance-4ec) diff --git a/challenge-338/sgreen/blog.txt b/challenge-338/sgreen/blog.txt new file mode 100644 index 0000000000..6e17a03487 --- /dev/null +++ b/challenge-338/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-the-highest-distance-4ec
\ No newline at end of file diff --git a/challenge-338/sgreen/perl/ch-1.pl b/challenge-338/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..e977ebb653 --- /dev/null +++ b/challenge-338/sgreen/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use JSON 'decode_json'; +use List::Util qw(max sum); + +sub main ($matrix) { + my $max = max( map { sum @$_ } @$matrix ); + say $max; +} + +main( decode_json( $ARGV[0] ) ); diff --git a/challenge-338/sgreen/perl/ch-2.pl b/challenge-338/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..e949a2bcd7 --- /dev/null +++ b/challenge-338/sgreen/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@arrays) { + my @arr1 = split /\D+/, $arrays[0]; + my @arr2 = split /\D+/, $arrays[1]; + + my $max = 0; + foreach my $a1 (@arr1) { + foreach my $a2 (@arr2) { + my $dist = abs( $a1 - $a2 ); + $max = $dist if $dist > $max; + } + } + say $max; +} + +main(@ARGV); diff --git a/challenge-338/sgreen/python/ch-1.py b/challenge-338/sgreen/python/ch-1.py new file mode 100755 index 0000000000..f91a214ebe --- /dev/null +++ b/challenge-338/sgreen/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def highest_row(matrix: list[list[int]]) -> int: + """Return the highest sum of any row in the matrix. + + Args: + matrix: A list of lists of integers. + + Returns: + The highest sum of any row in the matrix. + """ + return max(sum(row) for row in matrix) + + +def main(): + # Convert input into a matrix + matrix = json.loads(sys.argv[1]) + result = highest_row(matrix) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-338/sgreen/python/ch-2.py b/challenge-338/sgreen/python/ch-2.py new file mode 100755 index 0000000000..10dfb99729 --- /dev/null +++ b/challenge-338/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def max_distance(arr1: list[int], arr2: list[int]) -> int: + """Return the highest distance between any two integers in the lists. + + Args: + arr1: A list of integers. + arr2: A list of integers. + + Returns: + The highest distance between any two integers in the lists. + """ + return max(abs(a - b) for a in arr1 for b in arr2) + + +def main(): + # Convert input into integers. + arr1 = [int(n) for n in re.split(r'\D+', sys.argv[1])] + arr2 = [int(n) for n in re.split(r'\D+', sys.argv[2])] + result = max_distance(arr1, arr2) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-338/sgreen/python/test.py b/challenge-338/sgreen/python/test.py new file mode 100755 index 0000000000..e95c5c606d --- /dev/null +++ b/challenge-338/sgreen/python/test.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + matrix_1 = [ + [4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9] + ] + + matrix_2 = [[1, 5],[7, 3], [3, 5]] + matrix_3 = [[1, 2, 3], [3, 2, 1]] + matrix_4 = [[2, 8, 7], [7, 1, 3], [1, 9, 5]] + matrix_5 = [ + [10, 20, 30], + [5, 5, 5], + [0, 100, 0], + [25, 25, 25] + ] + + self.assertEqual(ch_1.highest_row(matrix_1), 16) + self.assertEqual(ch_1.highest_row(matrix_2), 10) + self.assertEqual(ch_1.highest_row(matrix_3), 6) + self.assertEqual(ch_1.highest_row(matrix_4), 17) + self.assertEqual(ch_1.highest_row(matrix_5), 100) + + def test_ch_2(self): + self.assertEqual(ch_2.max_distance([4, 5, 7],[9, 1, 3, 4]), 6) + self.assertEqual(ch_2.max_distance([2, 3, 5, 4],[3, 2, 5, 5, 8, 7]), 6) + self.assertEqual(ch_2.max_distance([2, 1, 11, 3],[2, 5, 10, 2]), 9) + self.assertEqual(ch_2.max_distance([1, 2, 3],[3, 2, 1]), 2) + self.assertEqual(ch_2.max_distance([1, 0, 2, 3],[5, 0]), 5) + + +if __name__ == '__main__': + unittest.main() |
