In an imaginary game, where four players (and only four) each roll a die, the player who rolls the largest number wins the round. This player receives 1
point, and they start the next round.
The original scores are represented as such: (0, 0, 0, 0)
where the first element is the score of the person who starts the first round of the game, the second element is the player who rolls second in the first round, and so on.
I want to write a function that is able to "clock up" the scores of each player, whereafter the final round it will return their scores with the original order in tact. We can assume that no two people roll the same number in one round.
It will take the rounds as a list of tuples, for example:
scores([(1, 3, 4, 6),(3, 5, 2, 1),(6, 3, 4, 2)])
>>> (2, 0, 0, 1)
scores([(6, 2, 3, 1),(5, 2, 3, 1),(6, 3, 2, 4),(4, 3, 2, 1)])
>>> (4, 0, 0, 0)
Note that the order of the final scores is correspondent with the first round.
(Imagine four people sitting in a circle, and if the third player wins the first round, they would start the second round, followed by the fourth player etc.)
def sumScores (scores):
points = [0, 0, 0, 0]
winner = 0
for score in scores:
winner = (winner + max(enumerate(score), key=lambda x: x[1])[0]) % 4
points[winner] += 1
return points
>>> sumScores([(1, 3, 4, 6),(3, 5, 2, 1),(6, 3, 4, 2)])
[2, 0, 0, 1]
>>> sumScores([(6, 2, 3, 1),(5, 2, 3, 1),(6, 3, 2, 4),(4, 3, 2, 1)])
[4, 0, 0, 0]