aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2019-06-23 23:54:36 -0400
committerDave Jacoby <jacoby.david@gmail.com>2019-06-23 23:54:36 -0400
commit10ddb787b5a4f3d4a9e60b89cc23b06d43048a60 (patch)
tree749c819ff3f274615c392ef8e43855a8336ab7fe
parent6f77e37cd4c56a8be6a4e9cfa223039312b6ac5f (diff)
downloadperlweeklychallenge-club-10ddb787b5a4f3d4a9e60b89cc23b06d43048a60.tar.gz
perlweeklychallenge-club-10ddb787b5a4f3d4a9e60b89cc23b06d43048a60.tar.bz2
perlweeklychallenge-club-10ddb787b5a4f3d4a9e60b89cc23b06d43048a60.zip
p14c2
-rw-r--r--challenge-014/dave-jacoby/p14c2.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-014/dave-jacoby/p14c2.pl b/challenge-014/dave-jacoby/p14c2.pl
new file mode 100644
index 0000000000..a36c3983e4
--- /dev/null
+++ b/challenge-014/dave-jacoby/p14c2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+use feature qw{ say };
+use strict;
+use warnings;
+
+# Using only the official postal (2-letter) abbreviations for the
+# 50 U.S. states, write a script to find the longest English word
+# you can spell? Here is the list of U.S. states abbreviations as
+# per wikipedia page. This challenge was proposed by team member
+# Neil Bowers.
+
+# using the %states hash makes for easy testing
+my %states = map { $_ => 1 } qw{
+ AL AK AZ AR CA CO CT DE FL GA
+ HI ID IL IN IA KS KY LA ME MD
+ MA MI MN MS MO MT NE NV NH NJ
+ NM NY NC ND OH OK OR PA RI SC
+ CD TN TX UT VT VA WA WV WI WY
+};
+
+# more universally available than my go-to dictionary DB
+my @words;
+if ( open my $fh, '<', '/usr/share/dict/words' ) {
+ @words = map { chomp; uc $_ } <$fh>;
+}
+
+my $longest = '';
+FOR: for my $word (@words) {
+ my @word = $word =~ /(\w{2})/g;
+
+ # there are apostrophes and unicode in that list
+ # this ensures that what we're looking at is valid.
+ my $join = join '', @word;
+ next unless $join eq $word;
+
+ # we check every letter pair and determine if it's in
+ # the states hash. we have labelled the outside for loop
+ # as FOR, so we can `next` on the outer loop, not just the
+ # inner loop.
+ for my $wo ( @word ) {
+ my $n = $states{$wo} ? 1 : 0;
+ next FOR unless $n;
+ }
+
+ # words that are not entirely made out of state abbreviations
+ # will not reach this point. And this will ignore words of
+ # identical size to the found longest, but I know they don't
+ # exist.
+ $longest = $word if length $longest < length $word;
+}
+say $longest;
+
+__DATA__
+
+CACOGALACTIA
+
+ noun In pathology, a bad condition of the milk.
+