-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle_funcs.py
144 lines (119 loc) · 5.07 KB
/
wordle_funcs.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Functions used by wordle solver.
"""
def globally_wrong_test(w, list_1):
"""
Tests a word/string (w) to see if any 'globally wrong' letters are present.
Returns true if NOT present. Returns False if present.
"""
matches = [letter for letter in list_1 if letter in w]
return not bool(matches)
def positionally_wrong_test(w, list_1):
"""
Tests a word/string (w) to see if any 'positionally wrong' letters are present.
Returns true if NOT present. Returns False if present.
"""
matches = [(let, ind) for ind, let in enumerate(w) if (let, ind) in list_1]
return not bool(matches)
def green_test(w, list_1, list_2):
"""
Tests a word/string (w) to see if 'correct' letters are present.
Starts with basic test of checking that each green letter of (list_1) is present in the word (w).
If basic test is passed, checks that position of letters within word are correct by comparing against enumerated
word (list_2).
Returns true if all currently identified green letters are present and in correct positions. Returns False
if any correctly identified are omitted or in the wrong position.
"""
for letter in list_1:
if letter not in w:
return False
matches = [(let, ind) for ind, let in enumerate(w) if (let, ind) in list_2]
return len(matches) == len(list_2)
def yellow_test(w, list_1, list_2):
"""
Tests a word/string (w) to see if yellow letters are present.
Returns true if all currently identified yes letters are present. Returns False if any correctly identified
are omitted from the word, OR if any yellow letters fall in positions where they are identified as positionally
incorrect.
"""
for letter in list_1:
if letter not in w:
return False
matches = [(let, ind) for ind, let in enumerate(w) if (let, ind) in list_2]
return not bool(matches)
def test_series(target, wrong, wrong_tups, green, green_tups, yellow, yellow_tups):
candidates = []
for word in target:
word = word.strip()
if globally_wrong_test(word, wrong):
if positionally_wrong_test(word, wrong_tups):
if green_test(word, green, green_tups):
if yellow_test(word, yellow, yellow_tups):
candidates.append(word)
return candidates
def determine_positionally_incorrect_letters(w, green, yellow, green_tups, yellow_tups):
multiples = [letter for letter in w if w.count(letter) > 1]
all_i = list(range(5))
gtpl = []
ytpl = []
good_i = []
bad_i = []
positionally_incorrect = []
def print_pi_values():
print(f'multiples: {multiples}')
print(f'greentupsperletter: {gtpl}')
print(f'yellowtupsperletter: {ytpl}')
print(f'good_i: {good_i}')
print(f'bad_i: {bad_i}')
print(f'positionally incorrect: {positionally_incorrect}')
if multiples:
for letter in list(set(multiples)):
if letter in green + yellow:
gtpl = [(let, i) for (let, i) in green_tups if let == letter]
ytpl = [(let, i) for (let, i) in yellow_tups if let == letter]
if multiples.count(letter) != len(gtpl) and not ytpl:
good_i = [i for (let, i) in gtpl]
bad_i = [i for i in all_i if i not in good_i]
positionally_incorrect += [(letter, i) for i in bad_i]
# print_pi_values()
return positionally_incorrect
def clue_input():
return input('Enter 5-letter string indicating color of each letters. '
'\n\"g\" for green, \"y\" for yellow, or \"b\" for black (e.g. ggybb): ')
def correct_answer(l):
"""
Intended to test the 'green_tups' list to see if the length is 5. If length is 5, function returns true, as
all correct letters/positions have been identified. Otherwise, returns false.
"""
return len(l) == 5
def print_guess_info(guess_num, user_guess, green, green_tups, yellow, yellow_tups, wrong, wrong_tups):
"""
Print function for internal variables for troubleshooting and development.
"""
print(f'Guess #{guess_num}:', user_guess)
print('Green letters: ', green)
print('Green letters tuple: ', green_tups)
print('Yellow letters: ', yellow)
print('Yellow letters tuple: ', yellow_tups)
print('Globally Wrong letters: ', wrong)
print('Limited wrong letters: ', wrong_tups, end='\n')
# def print_form(l, words_per_line):
# """
# Prints entries in list 'l' as an argument-specified words per line when solver is being used in python console.
# """
# if not l:
# print('No word matches found.')
# else:
# for i, word in enumerate(l):
# word = word.strip()
# print(word, end='\t')
# if (i + 1) % words_per_line == 0:
# print()
# def print_candidate_words(word_candidates):
# """
# Print function for solutions when wordle solver is operation within python console.
# """
# print('')
# print('Potential Solutions: ')
# print_form(word_candidates, 15)
# print('\n')