I have this code written along with the ‘Experiment’ function but I am not getting the correct Probability value. The experiment function is just returning 0.0 probability!
Here is my code:
import random
import copy
from collections import Counter
class Hat(object):
def __init__(self, **colorballs): # {'red': 2, 'blue': 1}
self.contents = []
for k, v in colorballs.items():
for i in range(v):
self.contents.append(k)
def draw(self, nos):
if nos > len(self.contents):
return self.contents
else:
all_balls = copy.deepcopy(self.contents)
pulled_out_balls = []
for i in range(0, nos):
pos = random.randint(0, len(all_balls)-1)
pulled_out_balls.append(all_balls.pop(pos))
return pulled_out_balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
group_counter = []
for exp in range(num_experiments):
group_counter.append(dict(Counter(hat.draw(num_balls_drawn))))
M = group_counter.count(expected_balls)
#print(group_counter)
# print(M)
# print(num_experiments)
probability = M/num_experiments
return probability
Could someone please point out what’s wrong in the ‘experiment’ function and what changes will fix this issue. TIA
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37
Challenge: Probability Calculator
Link to the challenge: