aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2025-01-28 15:57:23 -0800
committerrobbie-hatley <Robbie.Hatley@gmail.com>2025-01-28 15:57:23 -0800
commit8b130dff1dcf673614c39ea87185757cecb47757 (patch)
tree5e211203a59b788865e7763ff1480b6ea97a87db
parent9f01445472e1d7726cb68927820e322d56ca3142 (diff)
downloadperlweeklychallenge-club-8b130dff1dcf673614c39ea87185757cecb47757.tar.gz
perlweeklychallenge-club-8b130dff1dcf673614c39ea87185757cecb47757.tar.bz2
perlweeklychallenge-club-8b130dff1dcf673614c39ea87185757cecb47757.zip
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #306.
-rw-r--r--challenge-306/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-306/robbie-hatley/perl/ch-1.pl81
-rwxr-xr-xchallenge-306/robbie-hatley/perl/ch-2.pl94
3 files changed, 176 insertions, 0 deletions
diff --git a/challenge-306/robbie-hatley/blog.txt b/challenge-306/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..d827768106
--- /dev/null
+++ b/challenge-306/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2025/01/robbie-hatleys-solutions-in-perl-for_28.html \ No newline at end of file
diff --git a/challenge-306/robbie-hatley/perl/ch-1.pl b/challenge-306/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..c478230703
--- /dev/null
+++ b/challenge-306/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 306-1,
+written by Robbie Hatley on Mon Jan 27, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 306-1: "Odd Sum"
+Submitted by: Mohammad Sajid Anwar
+You are given an array of positive integers, @ints. Write a
+script to return the sum of all possible odd-length subarrays of
+the given array. A subarray is a contiguous subsequence of the
+array.
+
+Example #1:
+Input: @ints = (2, 5, 3, 6, 4)
+Output: 77
+Odd length sub-arrays:
+(2) => 2
+(5) => 5
+(3) => 3
+(6) => 6
+(4) => 4
+(2, 5, 3) => 10
+(5, 3, 6) => 14
+(3, 6, 4) => 13
+(2, 5, 3, 6, 4) => 20
+Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77
+
+Example #2:
+Input: @ints = (1, 3)
+Output: 4
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I'll solve this problem by using nested for loops to generate all valid odd-length blocks of contiguous
+indices for the given array, sum each with sum0 (from CPAN module "List::Util"), add each such sum to a
+variable "$sum", then return $sum.
+
+--------------------------------------------------------------------------------------------------------------
+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 real numbers, in proper Perl syntax, like so:
+./ch-1.pl '([-37.4,0.13,84.2,5.04,-3.92,6.666,-14.43], [-2,-1,0,1,2])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+use v5.36;
+use List::Util 'sum0';
+
+ # Return sum of sums of all odd-length subarrays
+ # of an array of numbers:
+ sub odd_sum ($aref) {
+ my $sum = 0;
+ for ( my $i = 0 ; $i <= $#$aref ; $i += 1 ) {
+ for ( my $j = $i ; $j <= $#$aref ; $j += 2 ) {
+ $sum += sum0(@$aref[$i..$j]);}}
+ return $sum;}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) : ([2, 5, 3, 6, 4], [1, 3]);
+# Expected outputs: 77 4
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ say '';
+ say "Array = @$aref";
+ say "Sum of sums of odd-length subarrays = ", odd_sum($aref);
+}
diff --git a/challenge-306/robbie-hatley/perl/ch-2.pl b/challenge-306/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..906f6ffc5b
--- /dev/null
+++ b/challenge-306/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 306-2,
+written by Robbie Hatley on Mon Jan 27, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 306-2: "Last Element"
+Submitted by: Mohammad Sajid Anwar
+You are given a array of integers, @ints. Write a script to play
+a game where you pick two biggest integers in the given array,
+say x and y. Then do the following:
+a) if x == y then remove both from the given array
+b) if x != y then remove x and replace y with (y - x)
+At the end of the game, there is at most one element left.
+Return the last element if found otherwise return 0.
+
+Example #1:
+Input: @ints = (3, 8, 5, 2, 9, 2)
+Output: 1
+Step 1: pick 8 and 9 => (3, 5, 2, 1, 2)
+Step 2: pick 3 and 5 => (2, 2, 1, 2)
+Step 3: pick 2 and 1 => (1, 2, 2)
+Step 4: pick 2 and 1 => (1, 2)
+Step 5: pick 1 and 2 => (1)
+
+Example #2:
+Input: @ints = (3, 2, 5)
+Output: 0
+Step 1: pick 3 and 5 => (2, 2)
+Step 2: pick 2 and 2 => ()
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+Since we're going to end up boiling each incoming array down to 1 or 0 elements, order doesn't matter, so I'll
+start by doing reverse numeric sort on the array. And each time an element is replaced, I'll sort the array
+again. That way, "the two biggest integers" will always be the first two elements. I'll then keep deleting and
+replacing as specified in the problem description, except that if the two largest integers are equal, I'll
+replace them with 0. That way, I'll always end up with exactly 1 integer left in the array (which may be
+zero or non-zero), and I'll present that integer as the output.
+
+By the way, I note that Example #1 doesn't obey the problem description's mandate "pick two biggest integers",
+as Steps 4 and 5 both pick "2 and 1" when in either case the two biggest integers would have been "2 and 2".
+The problem description doesn't specify "two biggest UNIQUE integers", so I elect to consider steps 4 and 5 of
+Example #1 to be "in-error" rather than re-write the problem description.
+
+I also note that by following the problem description as-written (ie, "the two biggest integers present,
+whether unique or identical"), in Example 1 the answer 1 is also obtained, though in a very different way.
+
+--------------------------------------------------------------------------------------------------------------
+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 integers, in proper Perl syntax, like so:
+./ch-2.pl '([-42,17,73,-84, 56],[-3,-2,-1,0,1,2,3]),[7,7,7,7,]'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+use v5.36;
+
+ # Keep replacing the two largest integers in an array
+ # with the absolute value of their difference until
+ # only one integer is left, then return that integer:
+ sub last_element ($aref) {
+ my @array = @$aref;
+ while (scalar(@array) > 1) {
+ @array = sort {$b<=>$a} @array;
+ my $y = shift @array;
+ my $x = shift @array;
+ unshift @array, $y-$x;}
+ return $array[0];}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) : ([3, 8, 5, 2, 9, 2],[3, 2, 5]);
+# Expected outputs: 1 0
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ say '';
+ say "Array = (@$aref)";
+ my $l_e = last_element($aref);
+ say "Last Element = $l_e";
+}