From b4191c7661cfdc1f71ecbb6fe2d05989c6839d42 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Thu, 16 Nov 2023 14:30:35 -0800 Subject: Robbie Hatley's refactored Perl solutions to PWCC 243. --- challenge-243/robbie-hatley/perl/ch-1.pl | 5 +++++ challenge-243/robbie-hatley/perl/ch-2.pl | 22 ++++++++++++++-------- 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 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; -- cgit From d6608839a8ec67a480ca9c0ba2af6336ceb82b85 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Thu, 16 Nov 2023 14:44:19 -0800 Subject: Fixed error in ch-1 --- challenge-243/robbie-hatley/perl/ch-1.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/challenge-243/robbie-hatley/perl/ch-1.pl b/challenge-243/robbie-hatley/perl/ch-1.pl index ccd2e03d59..d821bd0bef 100755 --- a/challenge-243/robbie-hatley/perl/ch-1.pl +++ b/challenge-243/robbie-hatley/perl/ch-1.pl @@ -72,8 +72,10 @@ BEGIN {$t0 = time} # 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; } -- cgit