Tell us what’s happening:
Getting two test module errors out of 3:
- Expected to reduce number of items from contents
- Different probability expected
Also, how do I use the copy module already included in the file?
Your code so far
import copy
import random
import collections
class Hat:
def init(self, **kwargs):
self.no_of_balls = kwargs
self.drawn_balls = list()
# print(self.no_of_balls)
self.contents = list()
for each_item in self.no_of_balls:
x = self.no_of_balls[each_item]
for each_colour in range(x):
self.contents.append(each_item)
def draw(self, draw_balls):
used_numbers = list()
self.drawn_balls = []
for each_time in range(draw_balls):
if draw_balls > len(self.contents):
self.drawn_balls = self.contents
break
y = random.randint(0, len(self.contents)-1)
temp = y
if y not in used_numbers:
self.drawn_balls.append(self.contents[y])
used_numbers.append(y)
else:
while temp == y:
y = random.randint(0, len(self.contents) - 1)
self.drawn_balls.append(self.contents[y])
used_numbers.append(y)
return self.drawn_balls
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
expected_balls_list = list()
total_events = num_experiments
favourable_events = 0
for each_item in expected_balls:
x = expected_balls[each_item]
for each_colour in range(x):
expected_balls_list.append(each_item)
for each_event in range(num_experiments):
for each_subevent in range(num_balls_drawn):
balls_drawn = hat.draw(each_subevent)
if collections.Counter(expected_balls_list) == collections.Counter(balls_drawn):
favourable_events += 1
else:
continue
probability = favourable_events/total_events
return probability
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
.
Challenge: Probability Calculator
Link to the challenge: