In MasterMind, one player has a secret code made from a sequence of colored pegs. Another player tries to guess the sequence. The player with the secret reports how many colors in the guess are in the secret and also reports whether the correct colors are in the correct place. Write a function report(guess, secret) that takes two lists and returns a 2-element list [number_right_ place, number_wrong_place] As an example, If the secret were red-red-yellow-yellow-black and the guess were red-red-red-green-yellow, then the secret holder would report that the guess identified three correct colors: two of the reds, both in the correct place, and one yellow, not in the correct place. In []: guess

Respuesta :

Answer:

#Python Program to solve the above illustration

# Comments are used for explanatory purpose

# define a function named game that take two parameters, player1 and player2

def Game(player1, player2):

# Record score

Score = [0, 0]

# Count trials

Trials = []

# Compare player1 guess with player2 hidden items

for i in range(len(player2)):

if player2[i]==player1[i]:

Trials.append(i)

#The above is done if player1 guessed correctly

#increase score by 1 below

Score[0] += 1

# Calculate trials and scores

for i in range(len(player2)):

for j in range(len(player2)):

if (not(j in Trials or i in Trials)) and player2[i]==player1[j]:

Score[1] += 1

Trials.append(j)

# Output scores

return Score

#Test program below

In []: player1 = ['yellow','yellow','yellow','blue','red']

In []: player2 = ['yellow','yellow','red','red','green']

In []: Game(player1, player2)