From acd58ab552396ab7dfddddceb486302f017bc3be Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Sat, 11 May 2024 13:01:15 -0700 Subject: Robbie Hatley's ammended Perl solutions to The Weekly Challenge #268. --- challenge-268/robbie-hatley/perl/ch-1.pl | 16 ++++++++-------- challenge-268/robbie-hatley/perl/ch-2.pl | 14 ++++++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index 73353171fb..ccf51e42d6 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -56,7 +56,7 @@ that common value is our "magic number", otherwise 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]])' +./ch-1.pl '([[3,8],[9,4,2]],[[3,8,17],[4,5,72]],[[1,2,3],[7,9,8]])' Output is to STDOUT and will be each input followed by the corresponding output. @@ -66,11 +66,11 @@ 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::SomeUtils 'pairwise'; use List::Util 'all'; - # Is a given scalar a reference to a Pair Of Same-Size Arrays Of Numbers? - sub is_possaon ($matref) { + # Is a given scalar a reference to a Pair Of Same-Size Arrays Of Integers? + sub is_possaoi ($matref) { 'ARRAY' ne ref $matref and return 0; 2 != scalar(@$matref) and return 0; scalar(@{$$matref[0]}) != scalar(@{$$matref[1]}) and return 0; @@ -82,12 +82,12 @@ Output is to STDOUT and will be each input followed by the corresponding output. return 1; } - # Determine "magic number" (if any) for given matrix: + # Return the "magic number" (if any) of a 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] + my @diff = pairwise {$b-$a} @row1, @row2; + all {$_ == $diff[0]} @diff and return $diff[0] or return 'none'; } @@ -123,7 +123,7 @@ for my $matref (@matrices) { say ''; say 'Matrix = '; say('[',join(', ', @$_),']') for @$matref; - !is_possaon($matref) + !is_possaoi($matref) and say 'Matrix is not a pair of same-size arrays of integers.' and say 'Moving on to next matrix.' and next; diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl index 690d1bb287..203785ce6f 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -61,20 +61,22 @@ Output is to STDOUT and will be each input followed by the corresponding output. # PRAGMAS, MODULES, AND SUBS: use v5.38; + use List::Util 'pairmap'; - # Is a given scalar a reference to an Array Of Integers? - sub is_aoi ($aref) { + # Is a given scalar a reference to an Even-Sized Array Of Integers? + sub is_esaoi ($aref) { 'ARRAY' ne ref $aref and return 0; + 0 != scalar(@$aref)%2 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: + # Return stairway sort of an array: sub stairway (@array) { my @sorted = sort {$a<=>$b} @array; - map {@sorted[2*$_+1,2*$_]} 0..($#sorted-1)/2; + pairmap {$b,$a} @sorted; } # ------------------------------------------------------------------------------------------------------------ @@ -99,8 +101,8 @@ my @arrays = @ARGV ? eval($ARGV[0]) : for my $aref (@arrays) { say ''; say 'Array = (', join(', ', @$aref), ')'; - !is_aoi($aref) - and say 'Error: Not an array of integers.' + !is_esaoi($aref) + and say 'Error: Not an even-sized array of integers.' and say 'Moving on to next array.' and next; say 'Stairway = (', join(', ', stairway(@$aref)), ')'; -- cgit From e5f3bc7f1000e693e2f8eefc59b802dfb559a442 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Sat, 11 May 2024 13:16:11 -0700 Subject: Further improvements to Robbie Hatley's Perl solutions for The Weekly Challenge #268. --- challenge-268/robbie-hatley/perl/ch-1.pl | 1 - challenge-268/robbie-hatley/perl/ch-2.pl | 13 ++++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index 091a5abd38..ccf51e42d6 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -44,7 +44,6 @@ 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 b0c388f978..2e59faae33 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -37,10 +37,14 @@ 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; + 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; } -------------------------------------------------------------------------------------------------------------- @@ -71,8 +75,7 @@ Output is to STDOUT and will be each input followed by the corresponding output. # Return stairway sort of an array: sub stairway (@array) { - my @sorted = sort {$a<=>$b} @array; - pairmap {$b,$a} @sorted; + pairmap {$b,$a} sort {$a<=>$b} @array; } # ------------------------------------------------------------------------------------------------------------ -- cgit From 2749df1c1b82cd174f3676c083418f2c8e8f8f8c Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Sat, 11 May 2024 13:23:45 -0700 Subject: Some error corrections for Robbie Hatley's Perl solutions for The Weekly Challenge #268. --- challenge-268/robbie-hatley/perl/ch-1.pl | 4 ++-- challenge-268/robbie-hatley/perl/ch-2.pl | 12 +++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index ccf51e42d6..8060986d2b 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -47,8 +47,8 @@ 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] + my @diff = pairwise {$b-$a} @row1, @row2; + all {$_ == $diff[0]} @diff and return $diff[0] or return 'none'; } diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl index 2e59faae33..a882accdce 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -34,17 +34,11 @@ 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: +This is equivalent to first sorting each array in increasing numeric order ("sort {$a<=>$b} @array"), +then swapping pairs: 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; + pairmap {$b,$a} sort {$a<=>$b} @array; } -------------------------------------------------------------------------------------------------------------- -- cgit