我需要编写一个主谋求解器,带有 4 个字母代码和字母 A 到 F。但是,我的猜测消除器留下了应该被消除的猜测。这是我的猜测消除器代码:
def getFeedback(self, feedbackStr):
if guesscount == 1:
import itertools #i know this shouldnt be here but homework comes with limitations
global guesslist
guesslist=list("".join(k) for k in itertools.product('ABCDEF', repeat=4))
guess_score=feedbackStr
for i in guesslist:
i_score=computeFeedback(guess, i)#compares possible code to current guess
if i_score != guess_score: #remove all non matching guesses
guesslist.remove(i)
return guesslist
这是计算机反馈代码:
def computeFeedback(code,guess):
# Given a code and guess computes the feedback string
b = 0
w = 0
inCodeCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
inGuessCount = {'A':0,'B':0,'C':0,'D':0, 'E':0, 'F':0}
for i in range(0,4):
if code[i] == guess [i]:
b += 1
inCodeCount[code[i]] += 1
inGuessCount[guess[i]] += 1
for ch in inCodeCount:
w += min(inCodeCount [ch], inGuessCount [ch])
w -= b
feedback = str(w)+'w'+str(b)+'b'
return feedback
但是,当 code = BBAA 并且第一个猜测是 AABB 时,例如,guesslist 是 ['BBAA', 'CCAE', 'DDBC', 'EECA', 'FFCE'] 我的程序应该消除除 BBAA 之外的所有内容,但它不是。它消除了 1290/1295 个错误的猜测,但一些错误的猜测似乎总是漏掉了。
根据输入的代码,胭脂错误猜测将始终相同。
我已经逐行浏览了它,例如,
computeFeedback('BBAA','AABB') <----- this is my guess vs the code
'4w0b'
computeFeedback('AABB','DDBC') <----- this is my guess vs a
possible code.
'0w1b'
computeFeedback('AABB','CCAE')
'1w0b'
computeFeedback('AABB','EECA')
'1w0b'
computeFeedback('AABB','FFCE')
'0w0b'
没有任何反馈字符串匹配,所以它们应该被消除。如果这个问题过于具体,我深表歉意,但我无法弄清楚为什么会发生这种情况。