aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Lied <boblied+github@gmail.com>2025-09-08 11:30:07 -0500
committerBob Lied <boblied+github@gmail.com>2025-09-08 11:30:07 -0500
commit0e17753853550838e534b6c47f8a8e8a8f3b0d8b (patch)
tree7c5b46a22084b58abcff2ea7f8d5f20df0be4530
parentcb150b4089bfaa27339dfd6bb4e51b580b12c32b (diff)
downloadperlweeklychallenge-club-0e17753853550838e534b6c47f8a8e8a8f3b0d8b.tar.gz
perlweeklychallenge-club-0e17753853550838e534b6c47f8a8e8a8f3b0d8b.tar.bz2
perlweeklychallenge-club-0e17753853550838e534b6c47f8a8e8a8f3b0d8b.zip
Week 338 solutions and blog reference
-rw-r--r--challenge-338/bob-lied/README.md8
-rw-r--r--challenge-338/bob-lied/blog.txt1
-rw-r--r--challenge-338/bob-lied/perl/ch-1.pl95
-rw-r--r--challenge-338/bob-lied/perl/ch-2.pl117
4 files changed, 217 insertions, 4 deletions
diff --git a/challenge-338/bob-lied/README.md b/challenge-338/bob-lied/README.md
index 85d3d50a7a..5247e8d471 100644
--- a/challenge-338/bob-lied/README.md
+++ b/challenge-338/bob-lied/README.md
@@ -1,5 +1,5 @@
-# Solutions to weekly challenge 337 by Bob Lied
+# Solutions to weekly challenge 338 by Bob Lied
-## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-337/)
-## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-337/bob-lied)
-[Blog](https://dev.to/boblied/)
+## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-338/)
+## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-338/bob-lied)
+[Blog](https://dev.to/boblied/pwc-338-maximal-maximization-of-maximums-4jm1)
diff --git a/challenge-338/bob-lied/blog.txt b/challenge-338/bob-lied/blog.txt
new file mode 100644
index 0000000000..4747245c03
--- /dev/null
+++ b/challenge-338/bob-lied/blog.txt
@@ -0,0 +1 @@
+https://dev.to/boblied/pwc-338-maximal-maximization-of-maximums-4jm1
diff --git a/challenge-338/bob-lied/perl/ch-1.pl b/challenge-338/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..9294f4ea67
--- /dev/null
+++ b/challenge-338/bob-lied/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2025, Bob Lied
+#=============================================================================
+# ch-1.pl Perl Weekly Challenge 338 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],
+# Output: 16 [10, 0, 0, 0],
+# [2, 2, 2, 9])
+# Example 2 Input: @matrix = ([1, 5],
+# Output: 10 [7, 3],
+# [3, 5])
+# Example 3 Input: @matrix = ([1, 2, 3],
+# Output: 6 [3, 2, 1])
+# Example 4 Input: @matrix = ([2, 8, 7],
+# Output: 17 [7, 1, 3],
+# [1, 9, 5])
+# Example 5 Input: @matrix = ([10, 20, 30],
+# Output: 100 [5, 5, 5],
+# [0, 100, 0],
+# [25, 25, 25])
+#=============================================================================
+
+use v5.42;
+use List::Util qw/sum0 max/;
+
+
+use Getopt::Long;
+my $Verbose = false;
+my $DoTest = false;
+my $Benchmark = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
+my $logger;
+{
+ use Log::Log4perl qw(:easy);
+ Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ),
+ layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" });
+ $logger = Log::Log4perl->get_logger();
+}
+#=============================================================================
+
+exit(!runTest()) if $DoTest;
+exit( runBenchmark($Benchmark) ) if $Benchmark;
+
+# Command line usage: give rows as comma-separated lists
+my @MATRIX = map { [ split(",", $_) ] } @ARGV;
+say highestRow(@MATRIX);
+
+#=============================================================================
+sub highestRow(@matrix)
+{
+ return max map { sum0 $_->@* } @matrix
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ my @matrix;
+ @matrix = ([4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]);
+ is( highestRow(@matrix), 16, "Example 1");
+
+ @matrix = ([1, 5], [7, 3], [3, 5]);
+ is( highestRow(@matrix), 10, "Example 2");
+
+ @matrix = ([1, 2, 3], [3, 2, 1]);
+ is( highestRow(@matrix), 6, "Example 3");
+
+ @matrix = ([2, 8, 7], [7, 1, 3], [1, 9, 5]);
+ is( highestRow(@matrix), 17, "Example 4");
+
+ @matrix = ([10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]);
+ is( highestRow(@matrix), 100, "Example 5");
+
+ @matrix = ([9]);
+ is( highestRow(@matrix), 9, "1x1");
+
+ @matrix = ([]);
+ is( highestRow(@matrix), 0, "Empty");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+
+ cmpthese($repeat, {
+ label => sub { },
+ });
+}
diff --git a/challenge-338/bob-lied/perl/ch-2.pl b/challenge-338/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..f1f2bc4da2
--- /dev/null
+++ b/challenge-338/bob-lied/perl/ch-2.pl
@@ -0,0 +1,117 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2025, Bob Lied
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge 338 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, 4, 6) = 6
+#
+# Example 2 Input: @arr1 = (2, 3, 5, 4)
+# @arr2 = (3, 2, 5, 5, 8, 7)
+# Output: 6
+# Example 3 Input: @arr1 = (2, 1, 11, 3)
+# @arr2 = (2, 5, 10, 2)
+# Output: 9
+# Example 4 Input: @arr1 = (1, 2, 3)
+# @arr2 = (3, 2, 1)
+# Output: 2
+# Example 5 Input: @arr1 = (1, 0, 2, 3)
+# @arr2 = (5, 0)
+# Output: 5#
+#
+# min1 max1
+# +-------------------------+
+# +--------+ +--------+ +--------+ +--------+ +--------+
+# min2 max2 min2 max2 min2 max2 min2 max2 min1 max2
+
+
+#=============================================================================
+
+use v5.42;
+
+
+use Getopt::Long;
+my $Verbose = false;
+my $DoTest = false;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+my $logger;
+{
+ use Log::Log4perl qw(:easy);
+ Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ),
+ layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" });
+ $logger = Log::Log4perl->get_logger();
+}
+#=============================================================================
+
+exit(!runTest()) if $DoTest;
+
+my @ARR1 = split(",", shift // "");
+my @ARR2 = split(",", shift // "");
+try { say maxDist(\@ARR1, \@ARR2); }
+catch ( $e ) { say STDERR "Problem: $e" }
+
+#=============================================================================
+sub maxDist($arr1, $arr2)
+{
+ use List::MoreUtils qw/minmax/;
+ use List::Util qw/max/;
+
+ die "ERROR empty array" if ( ! (@$arr1 && @$arr2) );
+
+ my ($min1, $max1) = minmax($arr1->@*);
+ my ($min2, $max2) = minmax($arr2->@*);
+
+ return max( $max1-$min2, $max2-$min1 );
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ my @arr1; my @arr2;
+ @arr1 = (4, 5, 7);
+ @arr2 = (9, 1, 3, 4);
+ is( maxDist(\@arr1, \@arr2), 6, "Example 1");
+
+ @arr1 = (2, 3, 5, 4);
+ @arr2 = (3, 2, 5, 5, 8, 7);
+ is( maxDist(\@arr1, \@arr2), 6, "Example 2");
+
+ @arr1 = (2, 1, 11, 3);
+ @arr2 = (2, 5, 10, 2);
+ is( maxDist(\@arr1, \@arr2), 9, "Example 3");
+
+ @arr1 = (1, 2, 3);
+ @arr2 = (3, 2, 1);
+ is( maxDist(\@arr1, \@arr2), 2, "Example 4");
+
+ @arr1 = (1, 0, 2, 3);
+ @arr2 = (5, 0);
+ is( maxDist(\@arr1, \@arr2), 5, "Example 5");
+
+ like( dies { maxDist([], []) }, qr/empty/i, "Empty arrays");
+
+ done_testing;
+}