Advertisement
mpettis

two-blue-stones-puzzle

Nov 21st, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import os, sys
  2. import random
  3.  
  4. random.seed(12345)
  5.  
  6.  
  7. stone_combinations = [('W', 'Y'), ('W', 'B1'), ('W', 'B2'), ('Y', 'B1'), ('Y', 'B2'), ('B1', 'B2')]
  8.  
  9. stone_permutations = [('W', 'Y'), ('W', 'B1'), ('W', 'B2'), ('Y', 'B1'), ('Y', 'B2'), ('B1', 'B2'),
  10. ('Y', 'W'), ('B1', 'W'), ('B2', 'W'), ('B1', 'Y'), ('B2', 'Y'), ('B2', 'B1')
  11. ]
  12.  
  13. def stoneColor(stone):
  14. """Pick stone color from entry -- needed because we represent two different blues as 'B1' and 'B2'"""
  15. return stone[0:1]
  16.  
  17.  
  18. def simIfAtLeastOneBlueCombo(trials):
  19. """Simulation: Pick combination of two, check both stones, proceed only if at least one of the stones is blue."""
  20. numTrialsAdmitted = 0
  21. numTrialsPassed = 0
  22. for i in xrange(trials):
  23. choice = random.choice(stone_combinations)
  24. if stoneColor(choice[0]) == 'B' or stoneColor(choice[1]) == 'B':
  25. numTrialsAdmitted += 1
  26. if choice == ('B1', 'B2'):
  27. numTrialsPassed += 1
  28. print "Simulation: Pick combination of two, check both stones, proceed only if at least one of the stones is blue."
  29. print "-"*80
  30. print "Total Sims: %d, Trials Admitted = %d, Trials Passed = %d, Percent of Passing over Admitted: %6.2f%%" % (trials, numTrialsAdmitted, numTrialsPassed, numTrialsPassed / float(numTrialsAdmitted) * 100 )
  31. print
  32.  
  33.  
  34.  
  35.  
  36.  
  37. def simIfRandomStoneIsBlueCombo(trials):
  38. """Simulation: Pick combination of two, randomly pick a stone, proceed only if that stone is blue."""
  39. numTrialsAdmitted = 0
  40. numTrialsPassed = 0
  41. for i in xrange(trials):
  42. choice = random.choice(stone_combinations)
  43. random_stone_from_choice = random.choice(choice)
  44. if stoneColor(random_stone_from_choice) == 'B':
  45. numTrialsAdmitted += 1
  46. if choice == ('B1', 'B2'):
  47. numTrialsPassed += 1
  48. print "Simulation: Pick combination of two, randomly pick a stone, proceed only if that stone is blue."
  49. print "-"*80
  50. print "Total Sims: %d, Trials Admitted = %d, Trials Passed = %d, Percent of Passing over Admitted: %6.2f%%" % (trials, numTrialsAdmitted, numTrialsPassed, numTrialsPassed / float(numTrialsAdmitted) * 100 )
  51. print
  52.  
  53.  
  54.  
  55. def simIfFirstBluePerm(trials):
  56. """Simulation: Pick permutation of two, check first stone, proceed only if first stone is blue."""
  57. numTrialsAdmitted = 0
  58. numTrialsPassed = 0
  59. for i in xrange(trials):
  60. choice = random.choice(stone_permutations)
  61. if stoneColor(choice[0]) == 'B':
  62. numTrialsAdmitted += 1
  63. if choice == ('B1', 'B2') or choice == ('B2', 'B1'):
  64. numTrialsPassed += 1
  65. print "Simulation: Pick permutation of two, check first stone, proceed only if first stone is blue."
  66. print "-"*80
  67. print "Total Sims: %d, Trials Admitted = %d, Trials Passed = %d, Percent of Passing over Admitted: %6.2f%%" % (trials, numTrialsAdmitted, numTrialsPassed, numTrialsPassed / float(numTrialsAdmitted) * 100 )
  68. print
  69.  
  70.  
  71.  
  72.  
  73.  
  74. # The simulations
  75. simIfAtLeastOneBlueCombo(10000)
  76. simIfRandomStoneIsBlueCombo(10000)
  77. simIfFirstBluePerm(10000)
  78.  
  79.  
  80. # ================================================================================
  81. # Output
  82. # ================================================================================
  83. # Simulation: Pick combination of two, check both stones, proceed only if at least one of the stones is blue.
  84. # --------------------------------------------------------------------------------
  85. # Total Sims: 10000, Trials Admitted = 8306, Trials Passed = 1651, Percent of Passing over Admitted: 19.88%
  86. #
  87. # Simulation: Pick combination of two, randomly pick a stone, proceed only if that stone is blue.
  88. # --------------------------------------------------------------------------------
  89. # Total Sims: 10000, Trials Admitted = 4967, Trials Passed = 1674, Percent of Passing over Admitted: 33.70%
  90. #
  91. # Simulation: Pick permutation of two, check first stone, proceed only if first stone is blue.
  92. # --------------------------------------------------------------------------------
  93. # Total Sims: 10000, Trials Admitted = 4892, Trials Passed = 1715, Percent of Passing over Admitted: 35.06%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement