我做过的第一个 interactive problem
给一个候选词列表,每次猜测可以猜里面的词,会返回猜中匹配的个数,
可以猜10次,
加上随机化策略之后几乎可以一定通过测试(尽管不是100%)
class Solution:
def findSecretWord(self, wordlist, master):
"""
:type wordlist: List[Str]
:type master: Master
:rtype: None
"""
def similar(X,Y):
return sum([X[i]==Y[i] for i in range(len(X))])
flag=False
guess_dict={}
random.shuffle(wordlist)
while(not flag):
for guess_word in wordlist:
guess=True
for guessed_word in guess_dict:
if similar(guessed_word,guess_word)!=guess_dict[guessed_word]:
guess=False
break
if guess:
print(guess_word)
guess_num=master.guess(guess_word)
if(guess_num==len(guess_word)):
flag=True
guess_dict[guess_word]=guess_num
break