diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-16 20:07:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-16 20:07:15 +0000 |
| commit | 6e984e69c9b32039b280f931650b1b7a5624d987 (patch) | |
| tree | 3d0986a7d1a9a1397412cb491d4caade4e6812f1 | |
| parent | df42df8b73d85878b0a58a345f1b9c089dc12e5e (diff) | |
| parent | b4480b09abb2ca4e8ab6fd05b0174594d8e93d0e (diff) | |
| download | perlweeklychallenge-club-6e984e69c9b32039b280f931650b1b7a5624d987.tar.gz perlweeklychallenge-club-6e984e69c9b32039b280f931650b1b7a5624d987.tar.bz2 perlweeklychallenge-club-6e984e69c9b32039b280f931650b1b7a5624d987.zip | |
Merge pull request #9592 from robbie-hatley/rh256
Minor tweaks to Robbie Hatley's Perl solutions for PWCC 256.
| -rwxr-xr-x | challenge-256/robbie-hatley/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-256/robbie-hatley/perl/ch-2.pl | 23 |
2 files changed, 32 insertions, 38 deletions
diff --git a/challenge-256/robbie-hatley/perl/ch-1.pl b/challenge-256/robbie-hatley/perl/ch-1.pl index aaefb38f49..300d439e64 100755 --- a/challenge-256/robbie-hatley/perl/ch-1.pl +++ b/challenge-256/robbie-hatley/perl/ch-1.pl @@ -35,59 +35,46 @@ Output: 2 -------------------------------------------------------------------------------------------------------------- PROBLEM NOTES: -This problem is easily solved by nested 3-part loops on index variables i and j such that j > i: - -# Reverse a string: -sub reverse_string ($x) { - return join '', reverse split //, $x -} - +This problem is easily solved using a nested pair of 3-part loops which compare the fold-case of each word +to the fold-case of the reverse of each word to its right; each time a match is found, increment a counter: +use v5.38; # How many fwd/rev pairs are in @$words? -sub pairs ($words) { - my $pairs = 0; +sub count_pairs ($words) { + my $pair_count = 0; for ( my $i = 0 ; $i <= $#$words - 1 ; ++$i ) { for ( my $j = $i + 1 ; $j <= $#$words - 0 ; ++$j ) { - if ( $$words[$i] eq reverse_string($$words[$j]) ) { - ++$pairs; + if ( fc $$words[$i] eq fc join '', reverse split //, $$words[$j] ) { + ++$pair_count; } } } - return $pairs; + return $pair_count; } -------------------------------------------------------------------------------------------------------------- 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 double-quoted words, in proper Perl syntax, like so: -./ch-1.pl '(["fish","beef","pork"],["yak", "rat", "tar", "kay"])' +./ch-1.pl '(["fish","beef","pork"],["Yak", "Rat", "Tar", "Kay"])' Output is to STDOUT and will be each input followed by the corresponding output. =cut # ------------------------------------------------------------------------------------------------------------ -# PRAGMAS AND MODULES: +# PRAGMAS, MODULES, AND SUBROUTINES: use v5.38; - -# ------------------------------------------------------------------------------------------------------------ -# SUBROUTINES: - -# Reverse a string: -sub reverse_string ($x) { - return join '', reverse split //, $x -} - # How many fwd/rev pairs are in @$words? -sub pairs ($words) { - my $pairs = 0; +sub count_pairs ($words) { + my $pair_count = 0; for ( my $i = 0 ; $i <= $#$words - 1 ; ++$i ) { - for ( my $j = $i + 1 ; $j <= $#$words ; ++$j ) { - if ( $$words[$i] eq reverse_string($$words[$j]) ) { - ++$pairs; + for ( my $j = $i + 1 ; $j <= $#$words - 0 ; ++$j ) { + if ( fc $$words[$i] eq fc join '', reverse split //, $$words[$j] ) { + ++$pair_count; } } } - return $pairs; + return $pair_count; } # ------------------------------------------------------------------------------------------------------------ @@ -112,5 +99,5 @@ my @arrays = @ARGV ? eval($ARGV[0]) : for my $aref (@arrays) { say ''; say 'Words: (', join(', ', map {"\"$_\""} @$aref), ')'; - say 'Number of fwd/rev pairs = ', pairs($aref); + say 'Number of fwd/rev pairs = ', count_pairs($aref); } diff --git a/challenge-256/robbie-hatley/perl/ch-2.pl b/challenge-256/robbie-hatley/perl/ch-2.pl index dbc3dea625..32474f6416 100755 --- a/challenge-256/robbie-hatley/perl/ch-2.pl +++ b/challenge-256/robbie-hatley/perl/ch-2.pl @@ -39,6 +39,16 @@ I could roll my own on this, but why? This problem is already solved by the "mes CPAN module "List::Util", so I'll just use that. Doing otherwise would be like engineering and manufacturing my own electric car at a cost of 5G$ when I could have bought a Tesla for 50k$. +One caveat with List::Util::mesh, though, is that it inserts undefs into its output if its two input strings +have unequal length. This can be got-around by using the "a//b" operator, which means "a if defined else b": + +use v5.38; +use List::Util 'mesh'; +sub merge_strings ($x, $y) { + return join '', map {$_ // ''} mesh [split //, $x], [split //, $y]; +} + + -------------------------------------------------------------------------------------------------------------- IO NOTES: Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a @@ -50,15 +60,12 @@ Output is to STDOUT and will be each input followed by the corresponding output. =cut # ------------------------------------------------------------------------------------------------------------ -# PRAGMAS AND MODULES USED: +# PRAGMAS, MODULES, AND SUBROUTINES: use v5.38; - -# ------------------------------------------------------------------------------------------------------------ -# SUBROUTINES: use List::Util 'mesh'; -no warnings 'uninitialized'; -sub mesh_strings ($x, $y) { - return join('', mesh([split //, $x], [split //, $y])); +# Merge two strings: +sub merge_strings ($x, $y) { + return join '', map {$_ // ''} mesh [split //, $x], [split //, $y]; } # ------------------------------------------------------------------------------------------------------------ @@ -83,5 +90,5 @@ my @pairs = @ARGV ? eval($ARGV[0]) : for my $pair_ref (@pairs) { say ''; say 'Strings: ', join(', ', map {"\"$_\""} @$pair_ref); - say 'Merged: ', join(', ', map {"\"$_\""} mesh_strings(@$pair_ref)); + say 'Merged: ', join(', ', map {"\"$_\""} merge_strings(@$pair_ref)); } |
