Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. import random
  2. from enum import Enum
  3.  
  4.  
  5. class CoinSide(Enum):
  6.     HEADS = 0
  7.     TAILS = 1
  8.  
  9.  
  10. class InterviewTallyByDay():
  11.     def __init__(self):
  12.         self.monday = InterviewTally()
  13.         self.tuesday = InterviewTally()
  14.  
  15.  
  16. class InterviewTally():
  17.     def __init__(self, heads=0, tails=0):
  18.         self.heads = heads
  19.         self.tails = tails
  20.  
  21.     def __add__(self, other):
  22.         return InterviewTally(
  23.             heads=self.heads + other.heads,
  24.             tails=self.tails + other.tails,
  25.         )
  26.  
  27.     def __iadd__(self, other):
  28.         self.heads += other.heads
  29.         self.tails += other.tails
  30.         return self
  31.  
  32.  
  33. def toss_coin():
  34.     return random.choice(list(CoinSide))
  35.  
  36.  
  37. interview_tallies_by_day = InterviewTallyByDay()
  38. for trial in range(int(1e6)):
  39.     coin_side = toss_coin()
  40.  
  41.     if coin_side == CoinSide.HEADS:
  42.         interview_tallies_by_day.monday += InterviewTally(heads=1)
  43.         # Not interviewed on Tuesday
  44.     elif coin_side == CoinSide.TAILS:
  45.         interview_tallies_by_day.monday += InterviewTally(tails=1)
  46.         interview_tallies_by_day.tuesday += InterviewTally(tails=1)
  47.  
  48. interview_tallies = interview_tallies_by_day.monday + \
  49.                     interview_tallies_by_day.tuesday
  50. print 'What your credence now for the proposition that the coin landed heads?'
  51. print interview_tallies.heads / float(interview_tallies.heads + interview_tallies.tails)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement