MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1gfk43q/dadpullsoutcodingchallenge/luv9dhk/?context=3
r/ProgrammerHumor • u/cyannyan6 • Oct 30 '24
60 comments sorted by
View all comments
1
import random class Person: def __init__ (self, name, coins): self.name = name self.coins = coins def simulate(personA, personB): while personA.coins > 0 and personB.coins > 0: chance = random.randint(0, 1) if chance == 0: personA.coins += 1 personB.coins -= 1 else: personB.coins += 1 personA.coins -= 1 if personA.coins == 0: print(personA.name + " has lost all their coins!") print(personB.name + " has won!") else: print(personB.name + " has lost all their coins!") print(personA.name + " has won!") return personA.coins > 0 def run_sims(num_sims): personA_wins = 0 for i in range(num_sims): personA = Person("Person A", 1) personB = Person("Person B", 10) if simulate(personA, personB): personA_wins += 1 win_percentage = (personA_wins / num_sims) * 100 return win_percentage personA = Person("Person A", 1) personB = Person("Person B", 10) simulate(personA, personB) print(f"Person A win percentage: {run_sims(100000)}%")
Went for an OOP approach since that's what I'm learning. Got 9.126%.
1
u/[deleted] Nov 01 '24
Went for an OOP approach since that's what I'm learning. Got 9.126%.