-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (50 loc) · 1.54 KB
/
main.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
import nltk
import re
def main():
nltk.download('words')
all_words = nltk.corpus.words.words()
word = word_entry(1)
check = check_word(word)
guesses = compile_guesses(all_words, word, check)
print_guesses(guesses)
def word_entry(n):
while True:
word = input(f"Enter word {n}: ")
if len(word) == 5:
return word
print("Length of word is not 5, please enter a 5 letter word")
def check_word(word):
print(word.upper())
print("\nEnter (G)reen, (Y)ellow, or (N)ot for each character:\n")
result = []
for c in word.upper():
inp = input(f"{c}: ").lower()
if inp != 'g' and inp != 'y' and inp !='n':
inp = 'n'
result.append(inp)
return result
def compile_guesses(all_words, word, check):
yellows = []
re_str = r'^'
for i, c in enumerate(word):
if check[i] == 'g': # Green (match)
re_str += c
elif check[i] == 'y': # Yellow (in word)
yellows.append(c)
re_str += r'\w'
else: # Unknown (any character)
re_str += r'\w'
re_str += r'$'
r = re.compile(re_str)
matched_words = list(filter(r.match, all_words))
for y in yellows:
matched_words = list(filter(lambda x: y in x, matched_words))
return matched_words
def print_guesses(guesses):
print("\nGuesses: ")
print("------------------------------")
for g in guesses:
print(g, end=' ')
print("\n------------------------------")
if __name__ == "__main__":
main()