aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2023-11-15 19:13:58 -0800
committerrobbie-hatley <Robbie.Hatley@gmail.com>2023-11-15 19:13:58 -0800
commit55325ce08c15b9645e46d2811a2041ba56472d42 (patch)
tree186ee47aef65636d265b6d0d817be26e561d3efc /challenge-243
parent928927686b98f953cedfbf54b325f5c9ea8c7cad (diff)
downloadperlweeklychallenge-club-55325ce08c15b9645e46d2811a2041ba56472d42.tar.gz
perlweeklychallenge-club-55325ce08c15b9645e46d2811a2041ba56472d42.tar.bz2
perlweeklychallenge-club-55325ce08c15b9645e46d2811a2041ba56472d42.zip
Improvements to 243-1 and 243-2.
Diffstat (limited to 'challenge-243')
-rwxr-xr-xchallenge-243/robbie-hatley/perl/ch-1.pl23
-rwxr-xr-xchallenge-243/robbie-hatley/perl/ch-2.pl7
2 files changed, 18 insertions, 12 deletions
diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl
index 828d630b59..20f03c220f 100755
--- a/challenge-243/robbie-hatley/perl/ch-1.pl
+++ b/challenge-243/robbie-hatley/perl/ch-1.pl
@@ -74,14 +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 ) {
- push @rp, [$$aref[$i], $$aref[$j]] if $$aref[$i] > 2 * $$aref[$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 @rpi, [$i, $j];
+ }
}
}
- return @rp
+ return @rpi
}
# ------------------------------------------------------------------------------------------------------------
@@ -107,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]);
}
}