aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-11-16 23:00:50 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-11-16 23:00:50 +0000
commit1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83 (patch)
tree9028f2035379b377fbdb954a5d429720849fb671
parent3378b0f87d8989aa973b351b592efceb0f7fa5eb (diff)
parentf3f58b32c6c1c807b74c12ce502546ee6bfe30a1 (diff)
downloadperlweeklychallenge-club-1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83.tar.gz
perlweeklychallenge-club-1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83.tar.bz2
perlweeklychallenge-club-1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rwxr-xr-xchallenge-243/robbie-hatley/perl/ch-1.pl11
-rwxr-xr-xchallenge-243/robbie-hatley/perl/ch-2.pl22
2 files changed, 23 insertions, 10 deletions
diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl
index 20f03c220f..d821bd0bef 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,18 @@ 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;
}
+# 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;