diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-16 22:23:39 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-16 22:23:39 +0000 |
| commit | 976af75105a156de9788a51c3c62391ac81995a5 (patch) | |
| tree | 619800f5508fc239c7855ba13aba7086c2bc60b3 | |
| parent | 46bef5068ef2a61feb02ee68942af6cb9b1bbafe (diff) | |
| parent | f7c30028a7ccdf505a038297ddf12f05dd7e79ae (diff) | |
| download | perlweeklychallenge-club-976af75105a156de9788a51c3c62391ac81995a5.tar.gz perlweeklychallenge-club-976af75105a156de9788a51c3c62391ac81995a5.tar.bz2 perlweeklychallenge-club-976af75105a156de9788a51c3c62391ac81995a5.zip | |
Merge pull request #9077 from robbie-hatley/243
Updated solutions by Robbie Hatley for Weekly Challenge 243.
| -rwxr-xr-x | challenge-243/robbie-hatley/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-243/robbie-hatley/perl/ch-2.pl | 7 |
2 files changed, 16 insertions, 12 deletions
diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl index 33fdb09fe1..20f03c220f 100755 --- a/challenge-243/robbie-hatley/perl/ch-1.pl +++ b/challenge-243/robbie-hatley/perl/ch-1.pl @@ -74,16 +74,16 @@ sub are_pos_ints ($aref) { return 1; } -sub reverse_pairs ($aref) { - my @rp = (); - for ( my $i = 0 ; $i <= $#$aref - 1 ; ++$i ) { - for ( my $j = $i + 1 ; $j <= $#$aref - 0 ; ++$j ) { +sub reverse_pair_indices ($aref) { + my @rpi = (); + for my $i ( 0 .. $#$aref - 1 ) { + for my $j ( $i + 1 .. $#$aref - 0 ) { if ( $$aref[$i] > 2 * $$aref[$j] ) { - push @rp, [$$aref[$i], $$aref[$j]]; + push @rpi, [$i, $j]; } } } - return @rp + return @rpi } # ------------------------------------------------------------------------------------------------------------ @@ -109,9 +109,12 @@ for my $aref (@arrays) { say 'Error: Not array of positive ints; skipping to next array.'; next; } - my @reverse_pairs = reverse_pairs($aref); - say 'Found ', scalar(@reverse_pairs), ' reverse pairs:'; - say '(', join(', ', map {'['.join(', ', @$_).']'} @reverse_pairs), ')'; + my @rpi = reverse_pair_indices($aref); + my $n = scalar @rpi; + say "Found $n reverse pairs", $n ? ':' : '.'; + for my $pair (@rpi) { + say 'Array[', $$pair[0], ', ', $$pair[1], '] = (', $$aref[$$pair[0]], ', ', $$aref[$$pair[1]], ')'; + } } exit; diff --git a/challenge-243/robbie-hatley/perl/ch-2.pl b/challenge-243/robbie-hatley/perl/ch-2.pl index cc410fa8b1..583fe8d6dc 100755 --- a/challenge-243/robbie-hatley/perl/ch-2.pl +++ b/challenge-243/robbie-hatley/perl/ch-2.pl @@ -43,11 +43,12 @@ Output: 49 PROBLEM NOTES: I'll use two three-part loops to sum all floors of pair quotients. + -------------------------------------------------------------------------------------------------------------- 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 positive integers, in proper Perl syntax, like so: -./ch-1.pl '([10,7,4,6,2],[7,6,5,7,6,5])' +./ch-2.pl '([10,7,4,6,2],[7,6,5,7,6,5])' Output is to STDOUT and will be each input array followed by the corresponding output. @@ -80,8 +81,8 @@ sub are_pos_ints ($aref) { sub quotient_floor_sum ($aref) { my $sum = 0; - for ( my $i = 0 ; $i <= $#$aref ; ++$i ) { - for ( my $j = 0 ; $j <= $#$aref ; ++$j ) { + for my $i ( 0 .. $#$aref ) { + for my $j ( 0 .. $#$aref ) { $sum += int($$aref[$i]/$$aref[$j]); } } |
