Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from enum import Enum
- class CoinSide(Enum):
- HEADS = 0
- TAILS = 1
- class InterviewTallyByDay():
- def __init__(self):
- self.monday = InterviewTally()
- self.tuesday = InterviewTally()
- class InterviewTally():
- def __init__(self, heads=0, tails=0):
- self.heads = heads
- self.tails = tails
- def __add__(self, other):
- return InterviewTally(
- heads=self.heads + other.heads,
- tails=self.tails + other.tails,
- )
- def __iadd__(self, other):
- self.heads += other.heads
- self.tails += other.tails
- return self
- def toss_coin():
- return random.choice(list(CoinSide))
- interview_tallies_by_day = InterviewTallyByDay()
- for trial in range(int(1e6)):
- coin_side = toss_coin()
- if coin_side == CoinSide.HEADS:
- interview_tallies_by_day.monday += InterviewTally(heads=1)
- # Not interviewed on Tuesday
- elif coin_side == CoinSide.TAILS:
- interview_tallies_by_day.monday += InterviewTally(tails=1)
- interview_tallies_by_day.tuesday += InterviewTally(tails=1)
- interview_tallies = interview_tallies_by_day.monday + \
- interview_tallies_by_day.tuesday
- print 'What your credence now for the proposition that the coin landed heads?'
- print interview_tallies.heads / float(interview_tallies.heads + interview_tallies.tails)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement