aboutsummaryrefslogtreecommitdiff
path: root/challenge-014/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorNiels van Dijke <65567640+PerlBoy1967@users.noreply.github.com>2021-11-28 23:27:52 +0100
committerGitHub <noreply@github.com>2021-11-28 23:27:52 +0100
commit9cc19213f6d3bd000f9580f8981ae6b79f202379 (patch)
treeb86967b049e6357bc69ee5e1999a29ad4ebbba99 /challenge-014/paulo-custodio/python/ch-2.py
parent87aa8152be4cf2d9b0c65caa07a9070bd5ceceea (diff)
parentfb0fe52f0a11011dc5baaee81034a888a044b811 (diff)
downloadperlweeklychallenge-club-9cc19213f6d3bd000f9580f8981ae6b79f202379.tar.gz
perlweeklychallenge-club-9cc19213f6d3bd000f9580f8981ae6b79f202379.tar.bz2
perlweeklychallenge-club-9cc19213f6d3bd000f9580f8981ae6b79f202379.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-014/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-014/paulo-custodio/python/ch-2.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/challenge-014/paulo-custodio/python/ch-2.py b/challenge-014/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..fd1890993e
--- /dev/null
+++ b/challenge-014/paulo-custodio/python/ch-2.py
@@ -0,0 +1,119 @@
+#!/usr/bin/python3
+
+# Challenge 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.
+#
+# For example,
+# Pennsylvania + Connecticut = PACT
+# Wisconsin + North Dakota = WIND
+# Maine + Alabama = MEAL
+# California + Louisiana + Massachusetts + Rhode Island = Calamari
+
+import re
+import sys
+
+us_states = {
+ 'AL':'Alabama',
+ 'AK':'Alaska',
+ 'AZ':'Arizona',
+ 'AR':'Arkansas',
+ 'CA':'California',
+ 'CO':'Colorado',
+ 'CT':'Connecticut',
+ 'DE':'Delaware',
+ 'FL':'Florida',
+ 'GA':'Georgia',
+ 'HI':'Hawaii',
+ 'ID':'Idaho',
+ 'IL':'Illinois',
+ 'IN':'Indiana',
+ 'IA':'Iowa',
+ 'KS':'Kansas',
+ 'KY':'Kentucky',
+ 'LA':'Louisiana',
+ 'ME':'Maine',
+ 'MD':'Maryland',
+ 'MA':'Massachusetts',
+ 'MI':'Michigan',
+ 'MN':'Minnesota',
+ 'MS':'Mississippi',
+ 'MO':'Missouri',
+ 'MT':'Montana',
+ 'NE':'Nebraska',
+ 'NV':'Nevada',
+ 'NH':'New Hampshire',
+ 'NJ':'New Jersey',
+ 'NM':'New Mexico',
+ 'NY':'New York',
+ 'NC':'North Carolina',
+ 'ND':'North Dakota',
+ 'OH':'Ohio',
+ 'OK':'Oklahoma',
+ 'OR':'Oregon',
+ 'PA':'Pennsylvania',
+ 'RI':'Rhode Island',
+ 'SC':'South Carolina',
+ 'SD':'South Dakota',
+ 'TN':'Tennessee',
+ 'TX':'Texas',
+ 'UT':'Utah',
+ 'VT':'Vermont',
+ 'VA':'Virginia',
+ 'WA':'Washington',
+ 'WV':'West Virginia',
+ 'WI':'Wisconsin',
+ 'WY':'Wyoming',
+}
+
+def read_file(filename):
+ with open(filename) as f:
+ return f.readlines()
+
+def read_words(lines):
+ words = []
+ for line in lines:
+ word = line.strip()
+ if not re.search(r"\W", word):
+ words.append(word)
+ return words
+
+def word_regexp():
+ state_abbr = []
+ for state in us_states:
+ state_abbr.append(state)
+ return r"(?i:^("+"|".join(state_abbr)+r")+$)"
+
+def words_like_states(words):
+ matched = []
+ regexp = word_regexp()
+ for word in words:
+ if re.match(regexp, word):
+ matched.append(word)
+ return matched
+
+def longest_words(words):
+ longest = []
+ size = 0
+ for word in words:
+ if len(word) > size:
+ longest = [word]
+ size = len(word)
+ elif len(word) == size:
+ longest.append(word)
+ return longest
+
+def word_to_states(word):
+ states = []
+ for i in range(0, len(word), 2):
+ cc = word[i:i+2].upper()
+ states.append(us_states[cc])
+ return " + ".join(states)
+
+words = longest_words(words_like_states(read_words(read_file("words.txt"))))
+for word in words:
+ print(word+" = "+word_to_states(word))