diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-05-11 13:16:11 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-05-11 13:16:11 -0700 |
| commit | e5f3bc7f1000e693e2f8eefc59b802dfb559a442 (patch) | |
| tree | b6fc756af84f11d2f7a935cb0ad9e1dfe51fcc01 | |
| parent | 5478e42143645df6830f1914543b640117a966ba (diff) | |
| download | perlweeklychallenge-club-e5f3bc7f1000e693e2f8eefc59b802dfb559a442.tar.gz perlweeklychallenge-club-e5f3bc7f1000e693e2f8eefc59b802dfb559a442.tar.bz2 perlweeklychallenge-club-e5f3bc7f1000e693e2f8eefc59b802dfb559a442.zip | |
Further improvements to Robbie Hatley's Perl solutions for The Weekly Challenge #268.
| -rwxr-xr-x | challenge-268/robbie-hatley/perl/ch-1.pl | 1 | ||||
| -rwxr-xr-x | 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; } # ------------------------------------------------------------------------------------------------------------ |
