The function should return 0.272, but I am getting different numbers around 0.17
It appears there are problems with the colorCount after doing some debugging
hat = prob_calculator.Hat(blue=3, red=2, green=6)
probability = prob_calculator.experiment(
hat=hat,
expected_balls={"blue": 2,
"green": 1},
num_balls_drawn=4,
num_experiments=1000)
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
hatContents = []
# move hat.contents to hatContents
for i in hat.contents:
hatContents.append(i)
# find count of expected balls
expectedBallsCount = 0
for i in range(num_experiments):
# choose random balls
chosenBalls = []
if num_balls_drawn > len(hatContents):
return 1.0
else:
for i in range(num_balls_drawn):
chosenBall = random.choice(hatContents)
chosenBalls.append(chosenBall)
hatContents.remove(chosenBall)
# replace hatContents
hatContents = []
for i in hat.contents:
hatContents.append(i)
# count the expected colors in chosen balls
colorCount = 0
for key, value in expected_balls.items():
if value >= chosenBalls.count(key):
colorCount += 1
# check if color count equals the expected ball colors
if colorCount == len(expected_balls):
expectedBallsCount += 1
return expectedBallsCount/num_experiments