From f68b891f303a0cb30ae091a8ed3d76fda1e4266f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Nov 2021 09:13:19 +0000 Subject: Add Python solution to challenge 14 --- challenge-014/paulo-custodio/python/ch-1.py | 40 ++++++++++ challenge-014/paulo-custodio/python/ch-2.py | 119 ++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 challenge-014/paulo-custodio/python/ch-1.py create mode 100644 challenge-014/paulo-custodio/python/ch-2.py (limited to 'challenge-014/paulo-custodio/python') diff --git a/challenge-014/paulo-custodio/python/ch-1.py b/challenge-014/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6852909991 --- /dev/null +++ b/challenge-014/paulo-custodio/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +# Challenge 014 +# +# Challenge #1 +# Write a script to generate Van Eck's sequence starts with 0. For more +# information, please check out wikipedia page. This challenge was proposed by +# team member Andrezgz. + +def van_eck_iter(): + hist = [] + # first two terms + hist.append(0) + yield hist[-1] + + hist.append(0) + yield hist[-1] + + while True: + found = False + for m in range(len(hist)-2, -1, -1): + if hist[m]==hist[-1]: + hist.append(len(hist)-1-m) + yield hist[-1] + found = True + break + if not found: + hist.append(0) + yield hist[-1] + +sep = "" +output = "" +count = 0 +for n in van_eck_iter(): + output += sep + str(n) + sep = ", " + count += 1 + if count > 96: + break +print(output) 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)) -- cgit