aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2023-11-16 14:30:35 -0800
committerrobbie-hatley <Robbie.Hatley@gmail.com>2023-11-16 14:30:35 -0800
commitb4191c7661cfdc1f71ecbb6fe2d05989c6839d42 (patch)
tree8450de86e8df5f46b3f68f115ebff01b0059f3d3 /challenge-243
parentf7c30028a7ccdf505a038297ddf12f05dd7e79ae (diff)
downloadperlweeklychallenge-club-b4191c7661cfdc1f71ecbb6fe2d05989c6839d42.tar.gz
perlweeklychallenge-club-b4191c7661cfdc1f71ecbb6fe2d05989c6839d42.tar.bz2
perlweeklychallenge-club-b4191c7661cfdc1f71ecbb6fe2d05989c6839d42.zip
Robbie Hatley's refactored Perl solutions to PWCC 243.
Diffstat (limited to 'challenge-243')
-rwxr-xr-xchallenge-243/robbie-hatley/perl/ch-1.pl5
-rwxr-xr-xchallenge-243/robbie-hatley/perl/ch-2.pl22
2 files changed, 19 insertions, 8 deletions
diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl
index 20f03c220f..ccd2e03d59 100755
--- a/challenge-243/robbie-hatley/perl/ch-1.pl
+++ b/challenge-243/robbie-hatley/perl/ch-1.pl
@@ -11,6 +11,7 @@ This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard
TITLE BLOCK:
Solutions in Perl for The Weekly Challenge 243-1.
Written by Robbie Hatley on Tue Nov 14, 2023.
+Refactored on Thu Nov 16, 2023.
--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
@@ -68,12 +69,16 @@ BEGIN {$t0 = time}
# ------------------------------------------------------------------------------------------------------------
# SUBROUTINES:
+# Is a given scalar a ref to an array
+# of positive integers?
sub are_pos_ints ($aref) {
return 0 if 'ARRAY' ne ref $aref;
for ( @$aref ) {return 0 unless $_ =~ m/^[1-9]\d*$/;}
return 1;
}
+# Return list of all index pairs [i,j] such that
+# i<j and array[i] > 2*array[j]:
sub reverse_pair_indices ($aref) {
my @rpi = ();
for my $i ( 0 .. $#$aref - 1 ) {
diff --git a/challenge-243/robbie-hatley/perl/ch-2.pl b/challenge-243/robbie-hatley/perl/ch-2.pl
index 583fe8d6dc..bc51024a86 100755
--- a/challenge-243/robbie-hatley/perl/ch-2.pl
+++ b/challenge-243/robbie-hatley/perl/ch-2.pl
@@ -11,6 +11,7 @@ This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard
TITLE BLOCK:
Solutions in Perl for The Weekly Challenge 243-2.
Written by Robbie Hatley on Tue Nov 14, 2023.
+Refactored on Thu Nov 16, 2023.
--------------------------------------------------------------------------------------------------------------
PROBLEM DESCRIPTION:
@@ -73,17 +74,23 @@ BEGIN {$t0 = time}
# ------------------------------------------------------------------------------------------------------------
# SUBROUTINES:
+# Is a given scalar a ref to an array
+# of positive integers?
sub are_pos_ints ($aref) {
- return 0 if 'ARRAY' ne ref $aref;
- for ( @$aref ) {return 0 unless $_ =~ m/^[1-9]\d*$/;}
+ return 0 unless 'ARRAY' eq ref $aref;
+ for (@$aref) {
+ return 0 unless $_ =~ m/^[1-9]\d*$/;
+ }
return 1;
}
-sub quotient_floor_sum ($aref) {
+# Return sum of floors of quotients of pairs
+# of elements of an array of positive integers:
+sub sfqp ($aref) {
my $sum = 0;
- for my $i ( 0 .. $#$aref ) {
- for my $j ( 0 .. $#$aref ) {
- $sum += int($$aref[$i]/$$aref[$j]);
+ for my $x (@$aref) {
+ for my $y (@$aref) {
+ $sum += int($x/$y);
}
}
return $sum;
@@ -112,8 +119,7 @@ for my $aref (@arrays) {
say 'Error: Not array of positive ints; skipping to next array.';
next;
}
- my $sum = quotient_floor_sum($aref);
- say 'Sum of floors of pair quotients = ', $sum;
+ say 'Sum of floors of quotients of pairs = ', sfqp($aref);
}
exit;