diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-13 00:27:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-13 00:27:49 +0100 |
| commit | c5fcf2a0860873e8486191af5fa2c9ef8a39001c (patch) | |
| tree | 9c79f9abcbfdb4e47bf32bf7e9297fa001ef6316 | |
| parent | d93ae1050ee585b55bf817f7ea15641de5879ae8 (diff) | |
| parent | 2749df1c1b82cd174f3676c083418f2c8e8f8f8c (diff) | |
| download | perlweeklychallenge-club-c5fcf2a0860873e8486191af5fa2c9ef8a39001c.tar.gz perlweeklychallenge-club-c5fcf2a0860873e8486191af5fa2c9ef8a39001c.tar.bz2 perlweeklychallenge-club-c5fcf2a0860873e8486191af5fa2c9ef8a39001c.zip | |
Merge pull request #10076 from robbie-hatley/rh268
Robbie Hatley's ammended Perl soultions for The Weekly Challenge #268.
| -rwxr-xr-x | challenge-268/robbie-hatley/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-268/robbie-hatley/perl/ch-2.pl | 23 |
2 files changed, 21 insertions, 23 deletions
diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index 5f7860b760..8060986d2b 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -44,12 +44,11 @@ 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] + my @diff = pairwise {$b-$a} @row1, @row2; + all {$_ == $diff[0]} @diff and return $diff[0] or return 'none'; } @@ -57,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. @@ -67,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; @@ -83,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'; } @@ -124,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 a97e2828e0..a882accdce 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -34,13 +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: - # 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; + pairmap {$b,$a} sort {$a<=>$b} @array; } -------------------------------------------------------------------------------------------------------------- @@ -57,20 +55,21 @@ 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} sort {$a<=>$b} @array; } # ------------------------------------------------------------------------------------------------------------ @@ -95,8 +94,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)), ')'; |
