2

我需要编写一个主谋求解器,带有 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'

没有任何反馈字符串匹配,所以它们应该被消除。如果这个问题过于具体,我深表歉意,但我无法弄清楚为什么会发生这种情况。

4

1 回答 1

2

你的问题在于getFeedback功能。

我怀疑因为您同时迭代并同时修改列表,所以您的代码表现得很不稳定。如果我将其更改为更实用的样式(这样它不会修改列表,而是不断返回一个新列表),它会按预期工作。

(注意:我从内部删除了一些代码,getFeedback因为它使测试有点困难。你说你有不寻常的约束,所以你可能需要更改它以重新添加猜测列表生成代码。让它更干净单独的功能,但我认为这是切线的。)

import itertools

def generateInitialGuesslist():
    return list("".join(k) for k in itertools.product('ABCDEF', repeat=4))

def getFeedback(guess, feedbackStr, guess_list):
    guess_score = feedbackStr
    new_guess_list = []
    for candidate in guess_list:
        candidate_score = computeFeedback(guess, candidate)
        if candidate_score == guess_score:        #remove all non matching guesses
            new_guess_list.append(candidate)
    return new_guess_list

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

guesslist = generateInitialGuesslist()
guesslist = getFeedback('AABB', '1w1b', guesslist)
print guesslist
print len(guesslist)

AABB我从猜测和ABCD正确答案开始对此进行了测试。您的代码指出,还有 208 个猜测剩余1w1b用作反馈,这与我自己的主谋求解器所说的相同。

于 2013-09-27T19:25:58.747 回答