fork download
  1. def any():
  2. '''Expected number of letters to get any hero from one of the pools'''
  3. expected = 0
  4. didnt_get = 1 # Odds of not getting it before a roll
  5.  
  6. for roll in range(1,25):
  7. got = roll/100 # 1% first roll, 2% second roll, etc
  8. expected += didnt_get*roll*got # Odds of getting it exactly this roll, times number of rolls
  9. didnt_get *= (1-got) # Didn't get before, didn't get it now
  10.  
  11. expected += didnt_get*25*1 # 100% chance to get in the 25th roll
  12. return expected
  13.  
  14. expected_any = any()
  15. print('Expected number of letters to get ANY hero: {:.2f}'.format(expected_any))
  16.  
  17. def specific(missing=10):
  18. '''Expected number of letters to get a specific hero from one of the pools'''
  19. expected = 0
  20. didnt_get = 1
  21.  
  22. for roll in range(1, missing+1):
  23. got = 1/(missing-roll+1)
  24. expected += didnt_get*(roll*expected_any)*got
  25. didnt_get *= (1-got)
  26.  
  27. return expected
  28.  
  29. def birthday_paradox(rolled_ladies=0, rolled_gentlemen=0):
  30. '''Expected number of letters to get any pair, alternating ladies and gentlemen'''
  31. expected = 0
  32. didnt_get = 1
  33.  
  34. for roll in range(0, 10):
  35. # Roll a lady
  36. got_lady = min(1, rolled_gentlemen/(10-rolled_ladies))
  37. expected += didnt_get*((2*roll+1)*expected_any)*got_lady
  38. didnt_get *= (1-got_lady)
  39. rolled_ladies += 1
  40.  
  41. # Roll a gentleman
  42. got_gentleman = min(1, rolled_ladies/(10-rolled_gentlemen))
  43. expected += didnt_get*((2*roll+2)*expected_any)*got_gentleman
  44. didnt_get *= (1-got_gentleman)
  45. rolled_gentlemen += 1
  46.  
  47. return expected
  48.  
  49. expected_pair = birthday_paradox()
  50. print('Expected number of letters to get ANY pair, alternating ladies and gentlemen: {:.2f}'.format(expected_pair))
  51.  
  52. expected_specific = specific()
  53. print('Expected number of letters to get a SPECIFIC hero: {:.2f}'.format(expected_specific))
  54. print('Expected number of letters to get a SPECIFIC pair: {:.2f}'.format(expected_specific + expected_specific))
  55.  
  56. print('Expected number of letters to get EVERYTHING: {:.2f}'.format(20*expected_any))
Success #stdin #stdout 0.02s 9128KB
stdin
Standard input is empty
stdout
Expected number of letters to get ANY hero: 12.11
Expected number of letters to get ANY pair, alternating ladies and gentlemen: 64.75
Expected number of letters to get a SPECIFIC hero: 66.61
Expected number of letters to get a SPECIFIC pair: 133.21
Expected number of letters to get EVERYTHING: 242.21