-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
executable file
·83 lines (60 loc) · 2.75 KB
/
solver.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
#!/usr/bin/env python3
# Original code by [email protected]
#
# The text file contains a non-exhaustive set of words, which means that some
# words that are valid for a Spelling Bee puzzle may not appear in the solver's
# output. Additionally, not all of the output words will be valid entries for
# the puzzle. The puzzles are curated to exclude highly obscure words, which are
# not systematically excluded by the solver.
import sys
def is_pangram(letters_list, word):
'''Takes in a list of strings for letters_list and a string for word.
Returns a string indicating whether the word is a pangram or not.'''
for letter in letters_list:
if letter not in word:
return ""
return "pangram"
def solve_spelling_bee(letters_list, center_letter):
'''Takes in a list of strings for letters_list and a single character string for center_letter.
Returns a list of words from word list which contain the center letter, have a length of at
least 4, and do not contain any unacceptable letters.'''
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z']
unacceptable_letters = [l for l in alphabet if l not in letters_list]
acceptable_words = []
word_file = open("/usr/share/dict/words", "r") #replace with your own file path
wordlist = []
for word in word_file:
wordlist.append(str(word).lower()[:-1])
blocklist_file = open("blocklist.txt", "r") #replace with your own file path
blocklist = {}
# hashing is much faster
for blockword in blocklist_file:
blocklist[str(blockword).lower()[:-1]] = 1
for word in wordlist:
if center_letter in word:
if word not in acceptable_words and not word in blocklist:
if len(word) > 3:
if any(l in unacceptable_letters for l in word) == False:
acceptable_words.append(word)
return acceptable_words
def main():
# make sure there are two arguemnts
if len(sys.argv) != 3:
print("Usage: python3 solver.py <letters> <center letter>")
sys.exit(1)
# convert the letters to a list
letters_list = list(sys.argv[1])
center_letter = sys.argv[2]
if len(sys.argv[2]) != 1:
print("Please enter 1 letter for center letter.")
sys.exit(1)
if len(letters_list) != 7:
print("Please enter 7 letters for letters-list.")
sys.exit(1)
solution = solve_spelling_bee(letters_list, center_letter)
for word in solution:
print("%-10s %d %s" % ( word.title(), len(word), is_pangram(letters_list, word)))
if __name__ == "__main__":
main()