import random
import copy
class Hat:
def init(self, **kwargs):
self.hat = kwargs
self.contents =
for key,value in kwargs.items():
for i in range(value):
self.contents.append(key)
def draw(self,times):
choices = []
cp_contents = copy.deepcopy(self.contents)
for i in range(times):
try:
a = random.choice(self.contents)
choices.append(a)
self.contents.remove(a)
except:
return cp_contents
return choices
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
N = 0
for i in range(num_experiments):
balls_drawn_l = hat.draw(num_balls_drawn)
balls_drawn = {}
for i in balls_drawn_l:
balls_drawn[i] = balls_drawn.get(i, 0) + 1
flag = []
for key,value in expected_balls.items():
try:
if value <= balls_drawn[key]:
flag.append(True)
else:
flag.append(False)
except:
flag.append(True)
if all(flag) == True:
N += 1
hat.contents.extend(balls_drawn_l)
return N/num_experiments