1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/env perl
# Author: Steven Wilson
# Date: 2019-06-25
# Week: 014
# Challenge: #2
#
# 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.
# https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations
#
# For example,
# Pennsylvania + Connecticut = PACT
# Wisconsin + North Dakota = WIND
# Maine + Alabama = MEAL
# California + Louisiana + Massachusetts + Rhode Island = Calamari
use strict;
use warnings;
use autodie qw / open close /;
use feature qw / say /;
use List::MoreUtils qw(uniq);
my %states = (
AK => 1,
AL => 1,
AR => 1,
AZ => 1,
CA => 1,
CO => 1,
CT => 1,
DE => 1,
FL => 1,
GA => 1,
HI => 1,
IA => 1,
ID => 1,
IL => 1,
IN => 1,
KS => 1,
KY => 1,
LA => 1,
MA => 1,
MD => 1,
ME => 1,
MI => 1,
MN => 1,
MO => 1,
MS => 1,
MT => 1,
NC => 1,
ND => 1,
NE => 1,
NH => 1,
NJ => 1,
NM => 1,
NV => 1,
NY => 1,
OH => 1,
OK => 1,
OR => 1,
PA => 1,
RI => 1,
SC => 1,
SD => 1,
TN => 1,
TX => 1,
UT => 1,
VA => 1,
VT => 1,
WA => 1,
WI => 1,
WV => 1,
WY => 1,
);
my @found_words;
my @sorted_found_words;
open my $fh, '<', 'english.txt';
while ( my $word = <$fh> ) {
chomp $word;
$word = uc $word;
if ( length $word < 4 || ( ( length $word ) % 2 ) != 0 ) {
next;
}
else {
my @pairs = ( $word =~ m/../g );
my $found = grep { !exists $states{$_} } @pairs;
if ( !$found ) {
push @found_words, $word;
}
}
}
close $fh;
@sorted_found_words = sort { length $a <=> length $b } uniq sort @found_words;
say "Found the words:";
for (@sorted_found_words) {
print "$_, ";
}
print "\n";
|