diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-10 09:06:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-10 09:06:01 +0100 |
| commit | a46e0917b55733105526d13e7d2d998916dbf46d (patch) | |
| tree | 6da7555f7c7e13ed9b06c3cc0bc8dc4bd740d802 | |
| parent | facfc9ec7489e9386c85209dfed4bf34d2bda556 (diff) | |
| parent | 44b12677ec01b32a9816c0675a6a07f6dbf980eb (diff) | |
| download | perlweeklychallenge-club-a46e0917b55733105526d13e7d2d998916dbf46d.tar.gz perlweeklychallenge-club-a46e0917b55733105526d13e7d2d998916dbf46d.tar.bz2 perlweeklychallenge-club-a46e0917b55733105526d13e7d2d998916dbf46d.zip | |
Merge pull request #12662 from robbie-hatley/rh338
Robbie Hatley's solutions, in perl for The Weekly Challenge #338.
| -rw-r--r-- | challenge-338/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-338/robbie-hatley/perl/ch-1.pl | 108 | ||||
| -rwxr-xr-x | challenge-338/robbie-hatley/perl/ch-2.pl | 104 |
3 files changed, 213 insertions, 0 deletions
diff --git a/challenge-338/robbie-hatley/blog.txt b/challenge-338/robbie-hatley/blog.txt new file mode 100644 index 0000000000..c712747721 --- /dev/null +++ b/challenge-338/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/09/robbie-hatleys-solutions-in-perl-for_9.html diff --git a/challenge-338/robbie-hatley/perl/ch-1.pl b/challenge-338/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..c24f687919 --- /dev/null +++ b/challenge-338/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,108 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 338-1, +written by Robbie Hatley on Tue Sep 09, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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 +); + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +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. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of matrices of real numbers, in proper Perl syntax, like so: + +./ch-1.pl '([[8.1,32.9,-14.7], [8.1,32.9,14.7]], [[3,9],[1,12],[2,10]])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use POSIX 'Inf'; + use List::Util 'sum0'; + + # What is the maximum row sum + # of a matrix of real numbers? + sub max_row_sum ($mref) { + my $h = scalar(@$mref); + my $max = -Inf; + for (0..$h-1) { + my $rs = sum0(@{$$mref[$_]}); + if ($rs > $max) {$max = $rs}} + $max} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @matrices = @ARGV ? eval($ARGV[0]) : +( + # 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 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $mref (@matrices) { + say ''; + my @row_strings; + for (@$mref) { + push @row_strings, "[@$_]"} + say "Matrix = "; + say for @row_strings; + my $mrs = max_row_sum($mref); + say "Max row sum = $mrs"} diff --git a/challenge-338/robbie-hatley/perl/ch-2.pl b/challenge-338/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..21d1afacbd --- /dev/null +++ b/challenge-338/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,104 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 338-2, +written by Robbie Hatley on Tue Sep 09, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +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 +); + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +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. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of two arrays of real numbers, in proper Perl syntax, like so: + +./ch-2.pl '([[2,4,6,8],[1,3,2,7]],[[8.2,4.1,16.9,9.3],[3.0,-6.4,42.2,19.5]])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + + # What is the maximum distance + # between any pair of one-number-each + # from two arrays of numbers? + sub max_dist ($apref) { + my $max = 0; + for my $x (@{$$apref[0]}) { + for my $y (@{$$apref[1]}) { + my $dist = abs($x-$y); + if ($dist>$max) {$max = $dist}}} + $max} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arraypairs = @ARGV ? eval($ARGV[0]) : +( + # 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 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $apref (@arraypairs) { + say ''; + say "Array #1: (@{$$apref[0]})"; + say "Array #2: (@{$$apref[1]})"; + my $md = max_dist($apref); + say "Max distance = $md"} |
