From 7de86f162f3cd1ff3de6f96dbd897f587de7586d Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 8 Sep 2025 23:03:06 +0200 Subject: Add solution 338 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-338/jeanluc2020/blog-1.txt | 1 + challenge-338/jeanluc2020/blog-2.txt | 1 + challenge-338/jeanluc2020/perl/ch-1.pl | 81 +++++++++++++ challenge-338/jeanluc2020/perl/ch-2.pl | 206 +++++++++++++++++++++++++++++++++ 4 files changed, 289 insertions(+) create mode 100644 challenge-338/jeanluc2020/blog-1.txt create mode 100644 challenge-338/jeanluc2020/blog-2.txt create mode 100755 challenge-338/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-338/jeanluc2020/perl/ch-2.pl diff --git a/challenge-338/jeanluc2020/blog-1.txt b/challenge-338/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..69ff91b76f --- /dev/null +++ b/challenge-338/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-338-1.html diff --git a/challenge-338/jeanluc2020/blog-2.txt b/challenge-338/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..4a2b4fcdc8 --- /dev/null +++ b/challenge-338/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-338-2.html diff --git a/challenge-338/jeanluc2020/perl/ch-1.pl b/challenge-338/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..49f1b0e0be --- /dev/null +++ b/challenge-338/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-338/#TASK1 +# +# Task 1: Highest Row +# =================== +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# This one is very simple with a little help of List::Util: +# We just create all the sums using sum() and calculate the +# maximum of those using max(). map() helps to put everything +# into a single line. + +use v5.36; +use List::Util qw(max sum); + +highest_row([4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]); +highest_row([1, 5], [7, 3], [3, 5]); +highest_row([1, 2, 3], [3, 2, 1]); +highest_row([2, 8, 7], [7, 1, 3], [1, 9, 5]); +highest_row([10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]); + +sub highest_row( @matrix ) { + say "Input: \@matrix = ("; + foreach my $line (@matrix) { + say " [" . join(", ", @$line) . "],"; + } + say " )"; + say "Output: " . max( map { sum(@{$_}) } @matrix); +} diff --git a/challenge-338/jeanluc2020/perl/ch-2.pl b/challenge-338/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..e312d677a2 --- /dev/null +++ b/challenge-338/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,206 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-338/#TASK2 +# +# Task 2: Max Distance +# ==================== +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# This one turns out to be two nested loops, calculating +# differences all the time, and keeping the maximum updated +# throughout. + +use v5.36; + +max_distance([4, 5, 7], [9, 1, 3, 4]); +max_distance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]); +max_distance([2, 1, 11, 3], [2, 5, 10, 2]); +max_distance([1, 2, 3], [3, 2, 1]); +max_distance([1, 0, 2, 3], [5, 0]); + +sub max_distance( $arr1, $arr2 ) { + say "Input: (" . join(", ", @$arr1) . "), (" . join(", ", @$arr2) . ")"; + my $max = 0; + foreach my $i (@$arr1) { + foreach my $j (@$arr2) { + my $t = abs($i - $j); + $max = $t > $max ? $t : $max; + } + } + say "Output: $max"; +} -- cgit