The evaluate_guess
function below returns the evaluation of a guess with respect to Mastermind game rules:
# sample secret code and guess
secret_code = ["a", "a", "c", "a"]
guess = ["c", "a", "a", "b"]
def evaluate_guess(secret_code, guess):
score = []
temp_list = secret_code.copy()
for i in range(len(secret_code)):
if guess[i] == temp_list[i]:
score.append(1)
temp_list[i] = None
elif guess[i] in temp_list:
score.append(0)
temp_list[temp_list.index(guess[i])] = None
score.sort(reverse=True)
return score
print(evaluate_guess(secret_code, guess))
>>> [1, 0, 0]
Without changing the essence of the algorithm: Can I implement the
evaluate_guess
function in a more Pythonic way? Is it possible to implement the algorithm in a purely functional fashion?Is there a more straightforward algorithm?