Skip to content

Commit b947e44

Browse files
authored
Update guess-the-word.py
1 parent 5365d29 commit b947e44

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Python/guess-the-word.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1-
# Time: O(n^2)
1+
# Time: O(n)
22
# Space: O(n)
33

44
import collections
55
import itertools
66

77

88
class Solution(object):
9+
def findSecretWord(self, wordlist, master):
10+
"""
11+
:type wordlist: List[Str]
12+
:type master: Master
13+
:rtype: None
14+
"""
15+
possible = range(len(wordlist))
16+
n = 0
17+
while n < 6:
18+
count = [collections.Counter(w[i] for w in wordlist) for i in xrange(6)]
19+
guess = max(possible, key=lambda x: sum(count[i][c] for i, c in enumerate(wordlist[x])))
20+
n = master.guess(wordlist[guess])
21+
possible = [j for j in possible if sum(a == b for a, b in itertools.izip(wordlist[guess], wordlist[j])) == n]
22+
23+
24+
# Time: O(n^2)
25+
# Space: O(n)
26+
class Solution2(object):
927
def findSecretWord(self, wordlist, master):
1028
"""
1129
:type wordlist: List[Str]
@@ -37,7 +55,7 @@ def solve(H, possible):
3755

3856
# Time: O(n^2)
3957
# Space: O(n)
40-
class Solution2(object):
58+
class Solution3(object):
4159
def findSecretWord(self, wordlist, master):
4260
"""
4361
:type wordlist: List[Str]

0 commit comments

Comments
 (0)