summaryrefslogtreecommitdiff
path: root/014/ch2.pl
diff options
context:
space:
mode:
authorGuillermo Ramos2019-06-30 15:35:54 +0200
committerGuillermo Ramos2019-06-30 15:36:10 +0200
commit61d3daa4027caff03630c96910623a75075b244a (patch)
treec93b1bd86e4f98c3ae6dc47e2be7e1ebdfd3d8c5 /014/ch2.pl
parentfb8cf0eb53b8bd8e525f57df5952ccb0616aad51 (diff)
downloadperlweekly-61d3daa4027caff03630c96910623a75075b244a.tar.gz
[014#2] Optimizations
Diffstat (limited to '014/ch2.pl')
-rwxr-xr-x014/ch2.pl6
1 files changed, 4 insertions, 2 deletions
diff --git a/014/ch2.pl b/014/ch2.pl
index ae9027f..33e3092 100755
--- a/014/ch2.pl
+++ b/014/ch2.pl
@@ -33,10 +33,12 @@ my $max_len = 0;
# Given a word, return whether it can be composed using state abbreviations
sub suitable {
- $_ =~ s/^\s+|\s+$//g; # Trim
+ $_ =~ s/\s+$//g; # Trim
my $len = length $_;
- return 0 if $len % 2 != 0; # Length must be even
+
+ # Discard shorter words or the ones which have odd number of letters
+ return 0 if $len < $max_len || $len % 2 != 0;
# Iterate word in 2-letter chunks checking they are valid abbreviations
foreach my $i (0 .. $len/2-1) {