-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguesstheWord.py
More file actions
35 lines (29 loc) · 937 Bytes
/
guesstheWord.py
File metadata and controls
35 lines (29 loc) · 937 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/guess-the-word/
# Author: Miao Zhang
# Date: 2021-03-17
# """
# This is Master's API interface.
# You should not implement it, or speculate about its implementation
# """
# class Master:
# def guess(self, word: str) -> int:
class Solution:
def findSecretWord(self, wordlist: List[str], master: 'Master') -> None:
def match(a, b):
res = 0
for i in range(len(a)):
if a[i] == b[i]: res += 1
return res
cnt = 0
i = 0
while i < 10 and cnt < 6:
guess = wordlist[random.randint(0, len(wordlist) - 1)]
cnt = master.guess(guess)
words = []
for w in wordlist:
if match(guess, w) == cnt:
words.append(w)
wordlist = words
i += 1