aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-11 14:11:08 +0100
committerGitHub <noreply@github.com>2024-05-11 14:11:08 +0100
commit5a23d92684bafea4448fe1906ec8aa4ff4cf6f00 (patch)
tree5e5dbf7d345dd9665d692d50b5a675e6b3e9a06a
parent2ddbdc4f6e10c7768edef916cf3384e32b95a128 (diff)
parent2cf5836157d13cfc2cf0d3d99b8a063a108342d5 (diff)
downloadperlweeklychallenge-club-5a23d92684bafea4448fe1906ec8aa4ff4cf6f00.tar.gz
perlweeklychallenge-club-5a23d92684bafea4448fe1906ec8aa4ff4cf6f00.tar.bz2
perlweeklychallenge-club-5a23d92684bafea4448fe1906ec8aa4ff4cf6f00.zip
Merge pull request #10068 from robbie-hatley/rh268
Robbie Hatley's Perl solutions to The Weekly Challenge #268.
-rw-r--r--challenge-268/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-268/robbie-hatley/perl/ch-1.pl132
-rwxr-xr-xchallenge-268/robbie-hatley/perl/ch-2.pl103
3 files changed, 236 insertions, 0 deletions
diff --git a/challenge-268/robbie-hatley/blog.txt b/challenge-268/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..e217e37a23
--- /dev/null
+++ b/challenge-268/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/05/robbie-hatleys-solutions-to-weekly.html \ No newline at end of file
diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..5f7860b760
--- /dev/null
+++ b/challenge-268/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,132 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 268-1,
+written by Robbie Hatley on Wed May 08, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 268-1: Magic Number
+Submitted by: Mohammad Sajid Anwar
+You are given two arrays of integers of same size, @x and @y.
+Write a script to find the magic number which when added to each
+element of the first array gives the second array. Element
+order is not important.
+
+Example 1:
+Input: @x = (3, 7, 5)
+ @y = (9, 5, 7)
+Output: 2
+The magic number is 2.
+@x = (3, 7, 5)
+ + 2 2 2
+@y = (5, 9, 7)
+
+Example 2:
+Input: @x = (1, 2, 1)
+ @y = (5, 4, 4)
+Output: 3
+The magic number is 3.
+@x = (1, 2, 1)
+ + 3 3 3
+@y = (5, 4, 4)
+
+Example 3:
+Input: @x = (2)
+ @y = (5)
+Output: 3
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I'll sort both arrays then subtract the second from the first. If all elements of the difference are the same,
+that common value is our "magic number", otherwise return "none":
+
+ # Determine "magic number" (if any) for given matrix:
+ sub magic ($matref) {
+ my @row1 = sort {$a<=>$b} @{$$matref[0]};
+ my @row2 = sort {$a<=>$b} @{$$matref[1]};
+ my @diff = map {$$_[1]-$$_[0]} zip6 @row1, @row2;
+ all {$diff[0] == $_} @diff and return $diff[0]
+ or return 'none';
+ }
+
+--------------------------------------------------------------------------------------------------------------
+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 pairs of same-size arrays of integers, in proper Perl syntax, like so:
+./ch-1.pl '([[1,2,3],[7,8,9]],[[3,8],[9,4,2]],[[3,8,17],[4,5,72]])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.38;
+ use List::MoreUtils 'zip6';
+ use List::Util 'all';
+
+ # Is a given scalar a reference to a Pair Of Same-Size Arrays Of Numbers?
+ sub is_possaon ($matref) {
+ 'ARRAY' ne ref $matref and return 0;
+ 2 != scalar(@$matref) and return 0;
+ scalar(@{$$matref[0]}) != scalar(@{$$matref[1]}) and return 0;
+ for my $rowref (@$matref) {
+ for my $element (@$rowref) {
+ $element !~ m/^-[1-9]\d*$|^0$|^[1-9]\d*$/ and return 0;
+ }
+ }
+ return 1;
+ }
+
+ # Determine "magic number" (if any) for given matrix:
+ sub magic ($matref) {
+ my @row1 = sort {$a<=>$b} @{$$matref[0]};
+ my @row2 = sort {$a<=>$b} @{$$matref[1]};
+ my @diff = map {$$_[1]-$$_[0]} zip6 @row1, @row2;
+ all {$diff[0] == $_} @diff and return $diff[0]
+ or return 'none';
+ }
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @matrices = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [
+ [3, 7, 5],
+ [9, 5, 7],
+ ],
+ # Expected Output: 2
+
+ # Example 2 Input:
+ [
+ [1, 2, 1],
+ [5, 4, 4],
+ ],
+ # Expected Output: 3
+
+ # Example 3 Input:
+ [
+ [2],
+ [5],
+ ],
+ # Expected Output: 3
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $matref (@matrices) {
+ say '';
+ say 'Matrix = ';
+ say('[',join(', ', @$_),']') for @$matref;
+ !is_possaon($matref)
+ and say 'Matrix is not a pair of same-size arrays of integers.'
+ and say 'Moving on to next matrix.'
+ and next;
+ say 'Magic number = ', magic($matref);
+}
diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..a97e2828e0
--- /dev/null
+++ b/challenge-268/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,103 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 268-2,
+written by Robbie Hatley on Wed May 08, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 268-2: Number Game
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints, with even number of
+elements. Write a script to create a new array made up of
+elements of the given array. Pick the two smallest integers and
+add it to new array in decreasing order i.e. high to low. Keep
+doing until the given array is empty.
+
+Example 1
+Input: @ints = (2, 5, 3, 4)
+Output: (3, 2, 5, 4)
+Round 1: we picked (2, 3) and push it to the new array (3, 2)
+Round 2: we picked the remaining (4, 5) and push it to the new
+array (5, 4)
+
+Example 2
+Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1)
+Output: (1, 1, 4, 3, 6, 4, 9, 6)
+
+Example 3
+Input: @ints = (1, 2, 2, 3)
+Output: (2, 1, 3, 2)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This is equivalent to first sorting each array in increasing numeric order ("sort {$a<=>$b} @array"), then
+swapping pairs. Something like this:
+
+ # Reorder array of ints into a zigzagging ascending stairway:
+ sub stairway (@array) {
+ my @sorted = sort {$a<=>$b} @array;
+ map {@sorted[2*$_+1,2*$_]} 0..($#sorted-1)/2;
+ }
+
+--------------------------------------------------------------------------------------------------------------
+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 '([-2.4,"dog",["can",7]],[7,6,5,4,3,2],[82,83,84,85,13,-7,5,8,-3,-14])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.38;
+
+ # Is a given scalar a reference to an Array Of Integers?
+ sub is_aoi ($aref) {
+ 'ARRAY' ne ref $aref and return 0;
+ for my $x (@$aref) {
+ $x !~ m/^-[1-9]\d*$|^0$|^[1-9]\d*$/ and return 0;
+ }
+ return 1;
+ }
+
+ # Reorder array of ints into a zigzagging ascending stairway:
+ sub stairway (@array) {
+ my @sorted = sort {$a<=>$b} @array;
+ map {@sorted[2*$_+1,2*$_]} 0..($#sorted-1)/2;
+ }
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ [2, 5, 3, 4],
+ # Expected Output: (3, 2, 5, 4)
+
+ # Example 2 Input:
+ [9, 4, 1, 3, 6, 4, 6, 1],
+ # Expected Output: (1, 1, 4, 3, 6, 4, 9, 6)
+
+ # Example 3 Input:
+ [1, 2, 2, 3],
+ # Expected Output: (2, 1, 3, 2)
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $aref (@arrays) {
+ say '';
+ say 'Array = (', join(', ', @$aref), ')';
+ !is_aoi($aref)
+ and say 'Error: Not an array of integers.'
+ and say 'Moving on to next array.'
+ and next;
+ say 'Stairway = (', join(', ', stairway(@$aref)), ')';
+}