Tell us what’s happening:
I am having trouble understanding what the draw method is asking me to do. If you draw more balls then there are available, is it asking us to return all the balls back to the hat and the value of empty draw list? Or is it asking us to the balls, as in, contents, as the return value of the draw method? The codes below are by no means done and still a working progress.
Your code so far
import copy
import random
class Hat:
def __init__(self, **kwargs):
self.contents = [ball for ball in kwargs for _ in range(kwargs[ball])]
def draw(self, number):
total = len(self.contents)
draw_list = []
if number > total:
draw_list = self.contents
self.contents = []
print(drawn_list)
return self.contents
else:
for _ in range(number):
random_num = random.randint(0, total-1)
draw_list.append(self.contents.pop(random_num))
total -= 1
# self.contents = temp_list
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
hat_copy = copy.deepcopy(hat)
expected_list = []
for ball in expected_balls:
for _ in range(expected_balls[ball]):
expected_list.append(ball)
M = 0
for _ in range(num_experiments):
match = []
drawn = hat_copy.draw(num_balls_drawn)
for ball in drawn:
if ball in expected_list:
match.append(ball)
if sorted(match) == sorted(expected_list):
M += 1
return (M/num_experiments)
hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat,
expected_balls={'red':2,'green':1},
num_balls_drawn=5,
num_experiments=2000)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project