From 1e4191cece0997f12dd8acba835c0af15fdbf140 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 8 May 2024 19:37:26 -0700 Subject: Robbie Hatley's Perl solutions to The Weekly Challenge #268. --- challenge-268/robbie-hatley/blog.txt | 1 + challenge-268/robbie-hatley/perl/ch-1.pl | 130 +++++++++++++++++++++++++++++++ challenge-268/robbie-hatley/perl/ch-2.pl | 112 ++++++++++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 challenge-268/robbie-hatley/blog.txt create mode 100755 challenge-268/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-268/robbie-hatley/perl/ch-2.pl 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..f597ebc58b --- /dev/null +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,130 @@ +#!/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": + +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; +} + +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..4bb77dc174 --- /dev/null +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,112 @@ +#!/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: + +sub stairway (@array) { + my @zigzag = sort {$a<=>$b} @array; + for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { + my $temp = $zigzag[$i]; + $zigzag[$i] = $zigzag[$i+1]; + $zigzag[$i+1] = $temp; + } + return @zigzag; +} + +-------------------------------------------------------------------------------------------------------------- +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 @zigzag = sort {$a<=>$b} @array; + for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { + my $temp = $zigzag[$i]; + $zigzag[$i] = $zigzag[$i+1]; + $zigzag[$i+1] = $temp; + } + return @zigzag; +} + +# ------------------------------------------------------------------------------------------------------------ +# 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)), ')'; +} -- cgit From 8a3c6cf93015da059f25c462cd2d0dc98c35305b Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 8 May 2024 19:46:55 -0700 Subject: Updates to my solutions to PWCC 268. --- challenge-268/robbie-hatley/perl/ch-1.pl | 15 ++++++++------- challenge-268/robbie-hatley/perl/ch-2.pl | 16 ++++++++-------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index f597ebc58b..82efc9d48b 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -44,13 +44,13 @@ 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": -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'; -} + 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: @@ -82,6 +82,7 @@ sub is_possaon ($matref) { 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]}; diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl index 4bb77dc174..a48e66a289 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -37,15 +37,15 @@ PROBLEM NOTES: This is equivalent to first sorting each array in increasing numeric order ("sort {$a<=>$b} @array"), then swapping pairs. Something like this: -sub stairway (@array) { - my @zigzag = sort {$a<=>$b} @array; - for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { - my $temp = $zigzag[$i]; - $zigzag[$i] = $zigzag[$i+1]; - $zigzag[$i+1] = $temp; + sub stairway (@array) { + my @zigzag = sort {$a<=>$b} @array; + for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { + my $temp = $zigzag[$i]; + $zigzag[$i] = $zigzag[$i+1]; + $zigzag[$i+1] = $temp; + } + return @zigzag; } - return @zigzag; -} -------------------------------------------------------------------------------------------------------------- IO NOTES: -- cgit From 6520342720d049560b6ae78fa41e6d23016308a8 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Thu, 9 May 2024 08:30:19 -0700 Subject: Updates to my Perl solutions for PWCC 268. --- challenge-268/robbie-hatley/perl/ch-1.pl | 44 ++++++++++++++++---------------- challenge-268/robbie-hatley/perl/ch-2.pl | 29 +++++++++------------ 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index 82efc9d48b..73353171fb 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -65,31 +65,31 @@ Output is to STDOUT and will be each input followed by the corresponding output. # ------------------------------------------------------------------------------------------------------------ # 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; + 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; } - 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'; -} + # 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: diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl index a48e66a289..690d1bb287 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -60,27 +60,22 @@ Output is to STDOUT and will be each input followed by the corresponding output. # ------------------------------------------------------------------------------------------------------------ # PRAGMAS, MODULES, AND SUBS: -use v5.38; + 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; + # 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; } - return 1; -} -# Reorder array of ints into a zigzagging ascending stairway: -sub stairway (@array) { - my @zigzag = sort {$a<=>$b} @array; - for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { - my $temp = $zigzag[$i]; - $zigzag[$i] = $zigzag[$i+1]; - $zigzag[$i+1] = $temp; + # 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; } - return @zigzag; -} # ------------------------------------------------------------------------------------------------------------ # INPUTS: -- cgit From 2cf5836157d13cfc2cf0d3d99b8a063a108342d5 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Fri, 10 May 2024 09:38:07 -0700 Subject: Changes to my Perl solutions to PWCC 268 from work, 2024-05-10_09-38. --- challenge-268/robbie-hatley/perl/ch-1.pl | 1 + challenge-268/robbie-hatley/perl/ch-2.pl | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index 73353171fb..5f7860b760 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -44,6 +44,7 @@ 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]}; diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl index 690d1bb287..a97e2828e0 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -37,14 +37,10 @@ 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 @zigzag = sort {$a<=>$b} @array; - for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { - my $temp = $zigzag[$i]; - $zigzag[$i] = $zigzag[$i+1]; - $zigzag[$i+1] = $temp; - } - return @zigzag; + my @sorted = sort {$a<=>$b} @array; + map {@sorted[2*$_+1,2*$_]} 0..($#sorted-1)/2; } -------------------------------------------------------------------------------------------------------------- -- cgit