Python Puzzles
As the host of a weekly dinner gathering that includes you and seven friends, you’ve come up with an interesting method to decide who will be the host for the upcoming dinner party.
After the meal, all attendees, including yourself, gather around a circular table. You, as the current host, initiate a game by flipping a fair coin. If the coin lands heads up, you hand the coin to the person sitting on your right; if it’s tails, you pass it to your left.
The person who gets the coin then repeats the process, flipping it and passing it to their right or left based on the result. This cycle continues until there is only one person left who has not yet received the coin.
This last remaining individual, who has not touched the coin, is announced as the winner and is given the responsibility to host the next dinner party.
Note, because you were the first to flip the coin in the game, you are immediately disqualified from the possibility of hosting the upcoming dinner party.
Each participant has an equal chance of becoming the next dinner party host.
Seat 0: 0 wins
Seat 1: 1046 wins
Seat 2: 1005 wins
Seat 3: 1023 wins
Seat 4: 967 wins
Seat 5: 997 wins
Seat 6: 1000 wins
Seat 7: 995 wins
Seat 8: 974 wins
Seat 9: 1008 wins
Seat 10: 985 wins
import random # Number of simulations num_simulations = 10000 # Initialize counts of wins for each seat wins = [0] * 11 for _ in range(num_simulations): # Initialize game has_had_prize = [False] * 11 # track who has had the prize prize = 0 # host starts with the prize has_had_prize[prize] = True while has_had_prize.count(False) > 1: # while more than one person has not had the prize # Flip coin if random.choice(['heads', 'tails']) == 'heads': # Pass to the left prize = (prize - 1) % 11 else: # Pass to the right prize = (prize + 1) % 11 has_had_prize[prize] = True # The winner is the last person who has not had the prize winner = has_had_prize.index(False) wins[winner] += 1 # Print counts of wins for each seat for i, win_count in enumerate(wins): print(f'Seat {i}: {win_count} wins')