Reputation:
The purpose of this function is to compare the number of times a color appears in the code and the number of times it appears in the guess to ultimately determine how many colors were guessed correctly. I am experiencing some errors with my current code, and I believe that the number of lines could be greatly simplified. More specifically, I think I am overcomplicating the task at hand.
Does anyone have a suggestion of a better way to tackle this problem? I am very new to programming and it would be greatly appreciated.
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
Upvotes: 1
Views: 73
Reputation: 5975
assuming we ignore colors missing from both input strings, here is a way:
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
Upvotes: 1
Reputation: 25779
Wow, that's a lot of code... Can be simplified to:
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))
No additional modules required. validColors
are not needed for this comparison, you should check against validColors
when users input their combination.
Upvotes: 0
Reputation: 5433
Maybe you can build up from something like this, using 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
This will give you the count of correct guesses, disregarding position.
Upvotes: 0
Reputation: 5660
Here’s one way to do it:
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
will count the number of each color in a word, given a list of colors to count from. This uses a list comprehension.
correctColorCount
explained:
map(lambda word: countColors(word, colors), (secretCode, guess))
This will run countColors with secretCode and guess. Equivalent to writing:
[countColors(secretCode, colors), countColors(guess, colors)]
zip(*....)
This will take them in pairs: the first one of each, then the second, etc. So, this will essentially create a list for each color:
[actualNumberOfTimes, guessedNumberOfTimes]
.
enumerate
is probably easier to understand here.
Upvotes: 0