My issue is that in the test cases, they call hat.contents and expect it to have changed.
So contents is expected to change, but somehow in the experiments function I am meant to have contents NOT change, otherwise by the 2nd out of 1000 experiments I will run out of balls.
My code does not seem to be linking properly, I will have it in the comments.
I am getting the correct probability, it is just the first test case of testing the amount of contents after drawing.
I am not looking for a straight answer, just any help pointing in the right direction would be greatly appreciated. Thank you!
from __future__ import annotations
import random
import copy
class Hat():
def __init__(self, **kargs):
self.population = {}
self.contents = []
for k, v in kargs.items():
self.population[k] = v
i = 0
while i < v:
self.contents.append(k)
i = i + 1
def draw(self, num_balls = 0):
balls_drawn = []
old_balls = self.contents.copy()
if num_balls >= len(self.contents):
return self.contents
balls_drawn = random.sample(self.contents, num_balls)
for i in balls_drawn:
self.contents.remove(i)
self.contents = old_balls
return balls_drawn
def experiment(hat : Hat, expected_balls = {}, num_balls_drawn = 0, num_experiments = 0):
m = 0
experiment_var = 0
n = 0
while m < num_experiments:
truth = True
draw_dic = {}
drawn_list = hat.draw(num_balls_drawn)
for i in drawn_list:
if i in list(draw_dic.keys()):
draw_dic[i] += 1
else:
draw_dic.update({i:1})
print(draw_dic)
q = 0
TRUELIST = []
while q < len(expected_balls):
index = list(expected_balls.keys())[q]
if list(expected_balls.keys())[q] in draw_dic.keys():
if list(expected_balls.values())[q] <= draw_dic[index]:
TRUELIST.append('YAY')
else:
TRUELIST.append(0)
else:
TRUELIST.append(0)
q += 1
for i in TRUELIST:
if i == 0:
truth = False
if truth == True:
experiment_var += 1
n += 1
m += 1
return(experiment_var / n)
hat : A hat object containing balls that should be copied inside the function.
You should use a fresh copy of the hat on every experiment. You shouldn’t be putting balls back.
The balls should not go back into the hat during the draw (or after the draw), similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, return all the balls.
I think I may be misinterpreting the draw method. Should the balls not return to the contents after the draw is completed?
For example, if hat = Hat(blue=2, green=2)
and I draw 2 balls, for (green, blue)
now my contents are (blue, green). Should it stay this way? What if I call draw again? Then there will be 0 balls left