Guess the Word LeetCode solution in Python

1
class Solution:
2
def findSecretWord(self, words: List[str], master: "Master") -> None:
3
k = 1 # for tracing the number of loops
4
matches = 0
5
blacklists = [[] for i in range(6)]
6

7
while matches != 6:
8
n = len(words)
9
r = random.randint(0, n - 1)
10
matches = master.guess(words[r])
11
key = words[r]
12
# print(k, n, r, matches, key)
13

14
words.pop(r)
15

16
if matches == 0:
17
for i in range(6):
18
blacklists[i].append(key[i])
19
# print(blacklists)
20

21
elif matches > 0 and matches < 6:
22
candidates = []
23
for i in range(n - 1):
24
count = 0
25
for j in range(6):
26
if words[i][j] not in blacklists[j] and words[i][j] == key[j]:
27
count += 1
28
if count >= matches:
29
candidates.append(words[i])
30

31
words = candidates.copy()
32
# print(words)
33

34
k += 1

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0