计算两个字符串中字符数的问题

时间:2017-02-20 21:20:20

标签: python python-3.x

此功能的目的是比较颜色在代码中出现的次数和它在猜测中出现的次数,以最终确定正确猜出的颜色数量。我在使用当前代码时遇到了一些错误,我相信可以大大简化行数。更具体地说,我认为我手头的任务过于复杂。

有没有人建议更好的方法来解决这个问题?我是编程新手,非常感谢。

validColors = ("R", "G", "B", "Y", "O", "P")

secretCode = "YBGG"
guess = "BYYG"

def correctColorCount(secretCode, guess, validColors):

    count = 0

    numRedGuess = guess.count("R")
    numRedCode = secretCode.count("R")
    sumR = numRedGuess + numRedCode

    numGreenGuess = guess.count("G")
    numGreenCode = secretCode.count("G")
    sumG = numGreenGuess + numGreenCode

    numBlueGuess = guess.count("B")
    numBlueCode = secretCode.count("B")
    sumB = numBlueGuess + numBlueCode

    numYellowGuess = guess.count("Y")
    numYellowCode = secretCode.count("Y")
    sumY = numYellowGuess + numBlueCode

    numOrangeGuess = guess.count("O")
    numOrangeCode = secretCode.count("O")
    sumO = numOrangeGuess + numOrangeCode

    numPurpleGuess = guess.count("P")
    numPurpleCode = secretCode.count("P")
    sumP = numPurpleGuess + numPurpleCode

    if numRedCode == numRedGuess and sumR != 0 and sumR != 1:
        count += 1
        if numRedGuess == 2:
            count += 1
        elif numRedGuess == 3:
            count += 2
        elif numRedGuess == 4:
            count += 3
    elif numRedGuess >= 1 and numRedCode >= 1 and sumR != 0 and sumR != 1:
        count += 1
    if numGreenCode == numGreenGuess and sumG != 0 and sumG != 1:
        count += 1
        if numGreenGuess == 2:
            count += 1
        elif numGreenGuess == 3:
            count += 2
        elif numGreenGuess == 4:
            count += 3
    elif numGreenGuess >= 1 and numGreenCode >= 1 and sumG != 0 and sumG != 1:
        count += 1
    if numBlueCode == numBlueGuess and sumB != 0 and sumB != 1:
        count += 1
        if numBlueGuess == 2:
            count += 1
        elif numBlueGuess == 3:
            count += 2
        elif numBlueGuess == 4:
            count += 3
    elif numBlueGuess >= 1 and numBlueCode >= 1 and sumB != 0 and sumB != 1:
        count += 1
    if numYellowCode == numYellowGuess and sumY != 0 and sumY != 1:
        count += 1
        if numYellowGuess == 2:
            count += 1
        elif numYellowGuess == 3:
            count += 2
        elif numYellowGuess == 4:
            count += 3
    elif numYellowGuess >= 1 and numYellowCode >= 1 and sumY != 0 and sumY != 1:
        count += 1
    if numOrangeCode == numOrangeGuess and sumO != 0 and sumO != 1:
        count += 1
        if numOrangeGuess == 2:
            count += 1
        elif numOrangeGuess == 3:
            count += 2
        elif numOrangeGuess == 4:
            count += 3
    elif numOrangeGuess >= 1 and numOrangeCode >= 1 and sumO != 0 and sumO != 1:
        count += 1
    if numPurpleCode == numPurpleGuess and sumP != 0 and sumP != 1:
        count += 1
        if numPurpleGuess == 2:
            count += 1
        elif numPurpleGuess == 3:
            count += 2
        elif numPurpleGuess == 4:
            count += 3
    elif numPurpleGuess >= 1 and numPurpleCode >= 1 and sumP != 0 and sumP != 1:
        count += 1

    return count

4 个答案:

答案 0 :(得分:1)

假设我们忽略了两个输入字符串中缺少的颜色,这是一种方式:

def correctColorCount(secretCode, guess, validColors):
    count = 0
    # for all the color characters
    for color in validColors:
        # if color count is not zero and same for both strings
        if secretCode.count(color) == guess.count(color) != 0:
            count += 1
    return count

答案 1 :(得分:0)

这是一种方法:

def countColors(word, colors):
    return (word.count(color) for color in colors)

def correctColorCount(secretCode, guess, colors):
    for index, (guessedColor, correctColor) in enumerate(zip(*map(lambda word: 
        countColors(word, colors), (secretCode, guess)))):
        color = colors[index]
        if guessedColor == correctColor:
            print("You guessed", color, "correctly!")
        else:
            print("You guessed", color, "incorrectly. You guessed", guessedColor, "but it was", str(correctColor) + ".")


>>> validColors = ("R", "G", "B", "Y", "O", "P")
>>> secretCode = "YBGG"
>>> guess = "BYYG"
>>> correctColorCount(secretCode, guess, validColors)
You guessed R correctly!
You guessed G incorrectly. You guessed 2 but it was 1.
You guessed B correctly!
You guessed Y incorrectly. You guessed 1 but it was 2.
You guessed O correctly!
You guessed P correctly!
在给定要计数的颜色列表的情况下,

countColors将计算单词中每种颜色的数量。这使用列表理解。

correctColorCount解释说:

map(lambda word: countColors(word, colors), (secretCode, guess))

这将使用secretCode和guess运行countColors。相当于写作:

[countColors(secretCode, colors), countColors(guess, colors)]

zip(*....)

这将成对使用它们:每个中的第一个,然后是第二个,等等。因此,这将基本上为每种颜色创建一个列表: [actualNumberOfTimes, guessedNumberOfTimes]

enumerate可能更容易理解here

答案 2 :(得分:0)

也许你可以使用collections.Counter

来构建类似的东西
from collections import Counter


def correct_color_count(secret, guess):
    correct = Counter(secret) & Counter(guess)
    return sum(correct.values())

correct_color_count(secret="YBGG", guess="BYYG")
>>> 3

这将为您提供正确猜测的计数,无视位置。

答案 3 :(得分:0)

哇,这是很多代码......可以简化为:

def correctColorCount(secretCode, guess):
    guess_list = list(guess)
    return len(guess) - sum(1 for color in secretCode 
                            if color not in guess_list or guess_list.remove(color))

无需额外的模块。此比较不需要validColors,您应该在用户输入组合时检查validColors