From 963eb9901b599278839391b810cb51ad02f368cb Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Fri, 1 Dec 2023 19:00:30 -0800 Subject: Updated 245-1. --- challenge-245/robbie-hatley/perl/ch-1.pl | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/challenge-245/robbie-hatley/perl/ch-1.pl b/challenge-245/robbie-hatley/perl/ch-1.pl index 1b3c6a819a..900ec850f9 100755 --- a/challenge-245/robbie-hatley/perl/ch-1.pl +++ b/challenge-245/robbie-hatley/perl/ch-1.pl @@ -67,16 +67,18 @@ BEGIN {$t0 = time} # ------------------------------------------------------------------------------------------------------------ # SUBROUTINES: -sub is_array_of_pos_ints($aref) { +sub is_array_of_numbers($aref) { return 0 if 'ARRAY' ne ref $aref; for (@$aref) { - return 0 if !/^[1-9]\d*$/; + return 0 if !/^0$|^-?0\.[0-9]+$|^-?[1-9]\d*(?:\.[0-9]+)$/; } return 1; } sub sort_array1_by_array2($aref1, $aref2) { - return @$aref1[sort{$$aref2[$a]<=>$$aref2[$b]}0..$#$aref1]; + is_array_of_numbers($aref2) + and return @$aref1[sort{$$aref2[$a]<=>$$aref2[$b]}0..$#$aref1] + or return @$aref1[sort{$$aref2[$a]cmp$$aref2[$b]}0..$#$aref1] } # ------------------------------------------------------------------------------------------------------------ @@ -112,11 +114,6 @@ for my $aref (@arrays) { say 'Moving on to next array.'; next; } - if ( !is_array_of_pos_ints($aref2) ) { - say 'Error: second subarray is not array of positive integers.'; - say 'Moving on to next array.'; - next; - } my @sorted = sort_array1_by_array2($aref1, $aref2); say 'Sorted = (' . join(', ', map {"'$_'"} @sorted) . ')'; } -- cgit From 1f42ea705a1cc228d3d46b50b6e3d2b6f0480d19 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Fri, 1 Dec 2023 19:09:30 -0800 Subject: Fixed RE error in 245-1. --- challenge-245/robbie-hatley/perl/ch-1.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-245/robbie-hatley/perl/ch-1.pl b/challenge-245/robbie-hatley/perl/ch-1.pl index 900ec850f9..daccc6014f 100755 --- a/challenge-245/robbie-hatley/perl/ch-1.pl +++ b/challenge-245/robbie-hatley/perl/ch-1.pl @@ -42,7 +42,7 @@ IO NOTES: Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a double-quoted array of arrays of two arrays, with the inner array pairs consisting of an array of single-quoted strings followed by an array of small positive integers (1-9), in proper Perl syntax, like so: -./ch-1.pl "([['Go','Lisp','AutoIt3','Logo'],[2, 1, 4, 3]],[['Awk','Cobol','Perl','Sed'],[3,4,1,2]])" +./ch-1.pl "([['Go','Lisp','AutoIt3','Logo'],[2, 1, 4, 3]],[['Awk','Cobol','Perl','Sed'],['golf','quaint','broad','easy']])" Output is to STDOUT and will be each input array followed by the corresponding output. @@ -70,7 +70,7 @@ BEGIN {$t0 = time} sub is_array_of_numbers($aref) { return 0 if 'ARRAY' ne ref $aref; for (@$aref) { - return 0 if !/^0$|^-?0\.[0-9]+$|^-?[1-9]\d*(?:\.[0-9]+)$/; + return 0 if !/^0$|^-?0\.[0-9]+$|^-?[1-9]\d*(?:\.[0-9]+)*$/; } return 1; } -- cgit From ca7692e8ee5d545956017631e72538527ebca12f Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Fri, 1 Dec 2023 19:16:25 -0800 Subject: Fixed RE error in 245-1. --- challenge-245/robbie-hatley/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-245/robbie-hatley/perl/ch-1.pl b/challenge-245/robbie-hatley/perl/ch-1.pl index daccc6014f..cb693b56a8 100755 --- a/challenge-245/robbie-hatley/perl/ch-1.pl +++ b/challenge-245/robbie-hatley/perl/ch-1.pl @@ -70,7 +70,7 @@ BEGIN {$t0 = time} sub is_array_of_numbers($aref) { return 0 if 'ARRAY' ne ref $aref; for (@$aref) { - return 0 if !/^0$|^-?0\.[0-9]+$|^-?[1-9]\d*(?:\.[0-9]+)*$/; + return 0 if !/^0$|^-?0\.\d+$|^-?[1-9]\d*(?:\.\d+)*$/; } return 1; } -- cgit From b141e31cfadd526a3c036527653b1c892ec51b97 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Fri, 1 Dec 2023 19:21:05 -0800 Subject: Fixed RE and sample-input issues in 245-1. --- challenge-245/robbie-hatley/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-245/robbie-hatley/perl/ch-1.pl b/challenge-245/robbie-hatley/perl/ch-1.pl index cb693b56a8..13310e8f45 100755 --- a/challenge-245/robbie-hatley/perl/ch-1.pl +++ b/challenge-245/robbie-hatley/perl/ch-1.pl @@ -42,7 +42,7 @@ IO NOTES: Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a double-quoted array of arrays of two arrays, with the inner array pairs consisting of an array of single-quoted strings followed by an array of small positive integers (1-9), in proper Perl syntax, like so: -./ch-1.pl "([['Go','Lisp','AutoIt3','Logo'],[2, 1, 4, 3]],[['Awk','Cobol','Perl','Sed'],['golf','quaint','broad','easy']])" +./ch-1.pl "([['Go','Lisp','AutoIt3','Logo'],[2, 19, 111, 3]],[['Awk','Cobol','Perl','Sed'],['golf','quaint','broad','easy']])" Output is to STDOUT and will be each input array followed by the corresponding output. -- cgit