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

标签 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

最佳答案

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

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

关于python - 计算两个字符串中的字符数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42354430/

相关文章:

python - 从 Python 列表中获取最多的 'diverse' 对集?

python-3.x - 根据时间列来回计数行

python - 如何从 C 到 Python 编写这个 for 循环?

python-3.x - 使用json通过套接字发送大数据

python - 重写addAction()来修改pyqt中的字符串和QIcon

python - 连接到名称中包含连字符的 Azure 数据库

python - 如何在 Flask 外使用 jinja2 及其 i18n 扩展(使用 babel)

python - 删除python中所有.csv文件内容

python - 如何获取带有空行的多行输入 - Python

python - 抓取电子邮件地址时无法删除不需要的东西