In PYTHON 3 implement function simul that takes as input an
In PYTHON 3 implement function simul() that takes as input an integer n and simulates n rounds
of Rock, Paper, Scissors between players Player 1 and Player 2. The player who wins the
most rounds wins the n-round game, with ties possible. Your function should print the result
of the game as shown.
>>> simul(1)
Player 1
>>> simul(1)
Tie
>>> simul(100)
Player 2
Solution
//Here is an equivalent function without the RPS narrative:
//Here is one that follows the story and is expanded to Rock, Paper, Scissors, Lizard, Spock:
| //Here is an equivalent function without the RPS narrative: import random def simul(n): score = sum([random.choice([-1, 0, 1]) for i in xrange(n)]) winner_idx = 0 if score > 0 \\ else 1 if score < 0 \\ else 2 print [\'Player 1\', \'Player 2\', \'Tie\'][winner_idx] //Here is one that follows the story and is expanded to Rock, Paper, Scissors, Lizard, Spock: import random def rand_RPSLK(): while True: yield random.choice(\'PSKLR\') def simul(n): p1 = rand_RPSLK() p2 = rand_RPSLK() choice = lambda px: \'PSKLR\'.index(next(px)) score = sum([[0,1,-1,1,-1][choice(p1)-choice(p2)] for i in xrange(n)]) winner_idx = 0 if score > 0 \\ else 1 if score < 0 \\ else 2 print [\'Player 1\', \'Player 2\', \'Tie\'][winner_idx] |
