-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conditions.py
123 lines (103 loc) · 4.67 KB
/
Conditions.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
from HelperFunctions import split
from collections import Counter
# Description: Gives the WORDLE colours based on guess and solution
# guess: the guess - string
# solution: the solution to the wordle - string
# Return: list of O, G or Y inplace of the WORDLE colours - list of chars
def give_conditions(guess, solution):
test_a = split(guess)
sol_a = split(solution)
# Direct Matches (Green)
for i, char in enumerate(test_a):
if char == sol_a[i]:
test_a[i] = 'G'
sol_a[i] = '&'
# In word. (Yellow)
for i, char in enumerate(test_a):
if char in sol_a:
test_a[i] = 'Y'
sol_a[sol_a.index(char)] = '&'
# Get rid of others (Grey)
for i, char in enumerate(test_a):
if char != 'G' and char != 'Y':
test_a[i] = 'O'
return test_a
# Description: Checks if two words are the same (ignoring '' chars of not found yet letters)
# word: the guess - list of chars
# correct_letters: correct word so far (based on green tiles) - list of chars
# Return: true if words are the same - bool
def compare_exact(word, correct_letters):
for i, char in enumerate(correct_letters):
if char != '':
if word[i] != char:
return False
return True
# Description: Checks if a word contains the incorrect placement characters
# word: the guess - list of chars
# incorrect_placement: in each spot, a list of chars that have incorrectly been placed there - list of list of chars
# Return: the word's compatibility with the placement - bool
def check_wrong_spot(word, incorrect_placement):
for i, spot in enumerate(incorrect_placement):
for char in spot:
if char not in word or word[i] == char:
return False
return True
# Description: Checks if a word contains any non-allowed chars
# word: the guess - list of chars
# alpha: list of allowed alphabet chars - list of chars
# Return: the word's compatibility with the placement - bool
def letters_ok(word, alpha):
for char in word:
if char not in alpha:
return False
return True
# Description: Checks if a word contains more than allowed multiple count for the solution
# word: the guess - list of chars
# no_multiples: list of letter and it's associated non-allowed occurrence number - list of tuple(char, int)
# Return: the word's compatibility with the placement - bool
def check_multiples(word, no_multiples):
occ_dict = Counter(word)
for char, num in occ_dict.items():
if num > 1:
for tuple in no_multiples:
if char == tuple[0] and num >= tuple[1]:
return False
return True
# Description: Updates the conditions based on the give_conditions function and the guess
# condition: condition list (O,G,Y) - list of chars
# guess: the guess - list of chars
# correct_placement: correct word so far (based on green tiles) - list of chars
# incorrect_placement: in each spot, a list of chars that have incorrectly been placed there - list of list of chars
# allowed_alpha: list of allowed alphabet chars - list of chars
# no_multiples: list of letter and it's associated non-allowed occurrence number - list of tuple(char, int)
# Return: the updated conditions - [list of chars, list of list of chars, list of chars, list of tuple(char, int)]
def update_conditions(condition, guess, correct_placement, incorrect_placement, allowed_alpha, no_multiples):
occurrences = Counter(guess) # Check how many of each character there is
# loop through each character in the guess
for i, char in enumerate(guess):
# Were there any green letters?
if condition[i] == 'G':
correct_placement[i] = char
# Were there any yellow letters?
elif condition[i] == 'Y':
incorrect_placement[i].append(char)
# Update the grey letter list
else:
try:
# IF there exists the same 2+ letters with one being special,
# add to multiple occurrence list (special case)
if occurrences[char] > 1:
no_multiples.add((char, occurrences[char]))
continue
# If char in the word in another spot DON'T remove
# If char in the word same spot DON'T remove
flag = False
for spot in incorrect_placement:
if char in spot:
flag = True
if char not in correct_placement and not flag:
allowed_alpha.remove(char)
# If already removed
except ValueError:
pass
return correct_placement, incorrect_placement, allowed_alpha, no_multiples