-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellingbee.py
123 lines (92 loc) · 2.99 KB
/
spellingbee.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import allWords
ALPHABET = list("abcdefghijklmnopqrstuvwxyz")
def bold_text(txt):
print("#" * (len(txt) + 4))
print("# " + txt + " #")
print("#" * (len(txt) + 4))
def get_words():
use_all = input_yes_or_no("Use more than just common words [Y/n]? ")
if use_all:
return allWords.get_all_words()
return allWords.get_common_words()
def get_file_path():
print("Enter a file path to save the outputted words to.")
print("The file does not have to exist, and it should likely end with '.txt'.")
try:
user_input = input("File path (leave blank for default): ")
assert len(user_input) > 0
return user_input
except:
return "spelling_bee_output.txt"
def input_yes_or_no(prompt):
try:
user_input = input(prompt)
if user_input[0].lower() == "n":
return False
except:
pass
return True
def input_yellow_letter():
print("Enter the mandatory letter (aka the 'yellow' letter).")
while True:
try:
user_input = input("Mandatory Letter: ")
letter = user_input.lower()[0]
assert letter in ALPHABET
return letter
except:
print("Enter a letter!")
def input_grey_letters():
print("Enter the optional letters (aka the 'grey' letters).")
letters = []
try:
user_input = input("Letters (ex: 'fdtale' without the ''s): ")
letters_input = user_input.lower()
for letter in letters_input:
assert letter in ALPHABET
letters.append(letter)
return letters
except:
print("Hint: you are doing something wrong (enter only letters)")
def is_good_word(word, yl, gls):
if len(word) < 4:
return False
letter_lst = list(word)
if yl not in word:
return False
letter_lst.remove(yl)
for gl in gls:
while gl in letter_lst:
letter_lst.remove(gl)
return len(letter_lst) == 0
def calc_possible_words(words, yl, gls):
good_words = []
for word in words:
if is_good_word(word, yl, gls):
good_words.append(word)
return good_words
def main():
print("\nPROGRAM STARTING...\n")
bold_text("Welcome to the Spelling Bee helper!")
print("\n")
words = get_words()
print("\nENTER INFORMATION...\n")
yl = input_yellow_letter()
print("\n")
gls = input_grey_letters()
print("\n")
output_path = get_file_path()
print("\nSEARCHING...")
good_words = calc_possible_words(words, yl, gls)
print("FINISHED SEARCHING...\n")
if len(good_words) > 0:
out_str = "\n".join(good_words)
with open(output_path, "w") as out_file:
out_file.write(out_str)
bold_text("%i words found:" % len(good_words))
for i in range(len(good_words)):
print("%i) %s" % (i + 1, good_words[i]))
else:
print("No words found. Did you mis-type or incorrectly enter information?")
print("\nPROGRAM EXITING...\n")
main()