Mastermind
def right_inwrongplace(userGuess, number): # Create a list of Boolean values representing correct guesses. # for exemple: if numbers = [1,2,3,4] and guess = [1,2,9,9], correct_places will be [True,True,False,False] correct_places = [True if v == number[i] else False for i, v in enumerate(userGuess)] # create a list with only the incorrect guesses. g = [v for i, v in enumerate(userGuess) if not correct_places[i]] # create a list with the numbers which weren't guessed correctly. n = [v for i, v in enumerate(number) if not correct_places[i]] #return the amount of guesses that are correct but in the wrong place. (the numbers that are in both lists) return len([i for i in g if i in n])
Here is what the above code is Doing:
1. Create a list of Boolean values representing correct guesses.
2. Create a list with only the incorrect guesses.
3. Create a list with the numbers which weren’t guessed correctly.
4. Return the amount of guesses that are correct but in the wrong place. (the numbers that are in both lists)