-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_0843_GuesstheWord.py
More file actions
56 lines (43 loc) · 1.5 KB
/
_0843_GuesstheWord.py
File metadata and controls
56 lines (43 loc) · 1.5 KB
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
#-----------------------------------------------------------------------------
# Runtime: 36ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
# """
# This is Master's API interface.
# You should not implement it, or speculate about its implementation
# """
# class Master:
# def guess(self, word: str) -> int:
import random
class Solution:
def findSecretWord(self, wordlist: [str], master: 'Master') -> None:
wordlistHashSet = tuple(set(wordlist))
for _ in range(10):
word = random.choice(wordlistHashSet)
result = master.guess(word)
if result == len(word):
break
wordlistHashSet = [ anotherWord for anotherWord in wordlistHashSet if self.__matchCount(word, anotherWord) == result]
def __matchCount(self, word1: str, word2: str) -> int:
count = 0
for i in range(len(word1)):
if word1[i] == word2[i]:
count += 1
return count
class Master:
def __init__(self, secret: str, wordlist: [str]):
self.secret = secret
self.wordlist = set(wordlist)
self.guessCount = 0
def guess(self, word: str) -> int:
self.guessCount += 1
if len(self.secret) != len(word):
return -1
if word not in self.wordlist:
return -1
count = 0
for i in range(len(word)):
if word[i] == self.secret[i]:
count += 1
return count