From 9cf346c117d9719705f4978e9072dd4e3eb68acd Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Tue, 16 Sep 2025 19:00:36 -0700 Subject: Robbie Hatley's solutions, in Perl, for The Weekly Challenge #339. --- challenge-339/robbie-hatley/blog.txt | 1 + challenge-339/robbie-hatley/perl/ch-1.pl | 113 +++++++++++++++++++++++++++++++ challenge-339/robbie-hatley/perl/ch-2.pl | 91 +++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 challenge-339/robbie-hatley/blog.txt create mode 100755 challenge-339/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-339/robbie-hatley/perl/ch-2.pl diff --git a/challenge-339/robbie-hatley/blog.txt b/challenge-339/robbie-hatley/blog.txt new file mode 100644 index 0000000000..4e711020ec --- /dev/null +++ b/challenge-339/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/09/robbie-hatleys-solutions-in-perl-for_16.html diff --git a/challenge-339/robbie-hatley/perl/ch-1.pl b/challenge-339/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..cc9ab50b69 --- /dev/null +++ b/challenge-339/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,113 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 339-1, +written by Robbie Hatley on Mon Sep 15, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 339-1: Max Diff +Submitted by: Mohammad Sajid Anwar +You are given an array of integers having four or more elements. +Write a script to find two pairs of numbers from this list (WITH +4 DISTINCT INDEXES WITHIN THE ARRAY) so that the difference +between their products is maximum. Return the max difference. + +With Two pairs (a, b) and (c, d), the product difference is +(a * b) - (c * d). + +Example 1 +Input: (5, 9, 3, 4, 6) +Output: 42 +Pair 1: (9, 6) +Pair 2: (3, 4) +Product Diff: (9 * 6) - (3 * 4) => 54 - 12 => 42 + +Example 2 +Input: (1, -2, 3, -4) +Output: 10 +Pair 1: (1, -2) +Pair 2: (3, -4) + +Example 3 +Input: (-3, -1, -2, -4) +Output: 10 +Pair 1: (-1, -2) +Pair 2: (-3, -4) + +Example 4 +Input: (10, 2, 0, 5, 1) +Output: 50 +Pair 1: (10, 5) +Pair 2: (0, 1) + +Example 5 +Input: @ints = (7, 8, 9, 10, 10) +Output: 44 +Pair 1: (10, 10) +Pair 2: (7, 8) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this problem, I generate all possible "pairs of pairs" of numbers from the array such that all four +numbers have unique indexes. I compute the difference of the products of the pairs, and keep track of the +maximum difference seen, then return the maximum. + +-------------------------------------------------------------------------------------------------------------- +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 4-or-more numbers, in proper Perl syntax, like so: + +./ch-1.pl '([3,82,-47,56,8],[3.13,7.41,3.62,7.85])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + use POSIX 'Inf'; + + # What is the maximum difference of products + # of any two pairs of numbers from an array? + sub max_diff ($aref) { + my $n = scalar(@$aref); + my $max = -Inf; + for my $i (0..$n-1) { + for my $j (0..$n-1) { + for my $k (0..$n-1) { + for my $l (0..$n-1) { + next if $i==$j || $i==$k || $i==$l + || $j==$k || $j==$l || $k==$l; + my $a = $$aref[$i]; my $b = $$aref[$j]; + my $c = $$aref[$k]; my $d = $$aref[$l]; + my $diff = $a*$b - $c*$d; + if ($diff > $max) {$max = $diff}}}}} + $max} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + [5, 9, 3, 4, 6] , # Expected output: 42 + [1, -2, 3, -4] , # Expected output: 10 + [-3, -1, -2, -4] , # Expected output: 10 + [10, 2, 0, 5, 1] , # Expected output: 50 + [7, 8, 9, 10, 10] , # Expected output: 44 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + say "Array = (@$aref)"; + my $md = max_diff($aref); + say "Maximum different of pair products = $md"; +} diff --git a/challenge-339/robbie-hatley/perl/ch-2.pl b/challenge-339/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..3eb92e680a --- /dev/null +++ b/challenge-339/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,91 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 339-2, +written by Robbie Hatley on Mon Sep 15, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 339-2: Peak Point +Submitted by: Mohammad Sajid Anwar +You are given an array of altitude changes. Write a script to +find the maximum altitude attained, assuming that one starts +at altitude 0. + +Example 1 +Input: [-5, 1, 5, -9, 2] +Output: 1 + + +Example 2 +Input: [10, 10, 10, -25] +Output: 30 + +Example 3 +Input: [3, -4, 2, 5, -6, 1] +Output: 6 + +Example 4 +Input: [-1, -2, -3, -4] +Output: 0 + +Example 5 +Input: [-10, 15, 5] +Output: 10 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this problem, I keep track of "current" and and "max" altitudes reached after each altitude change, +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 numbers, in proper Perl syntax, like so: + +./ch-2.pl '([-35.2, 38.7, -14.9, 13.9],[1,2,3,4,-1,-2,-3,-4])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # What is the maximum + # altitude attained? + sub max_alt ($aref) { + my $cur = 0; + my $max = 0; + for my $change (@$aref) { + $cur += $change; + if ($cur > $max) { + $max = $cur}} + $max} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + [-5, 1, 5, -9, 2] , # Expected output: 1 + [10, 10, 10, -25] , # Expected output: 30 + [3, -4, 2, 5, -6, 1] , # Expected output: 6 + [-1, -2, -3, -4] , # Expected output: 0 + [-10, 15, 5] , # Expected output: 10 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + say "Altitude changes = (@$aref)"; + my $tac = max_alt($aref); + say "Maximum altitude = $tac"; +} -- cgit