Jeremy,
Thanks for the answer. I was able to get through the questions but now i’m at the last bit: my probability is off by just a little bit.
I referred to this forum post that had the same issue but the hint you described there
still didn’t get me to realize what I am doing wrong. I am getting a 0.259 != 0.272 within 0.01 delta (0.013000000000000012 difference) : Expected experiment method to return a different probability.
below is my code:
import copy
import random
# Consider using the modules imported above.
class Hat:
contents = []
drawn = []
def __init__(self,**data):
v = 0
self.contents.clear()
self.drawn.clear()
for key,value in data.items():
while v < value:
self.contents.append(key)
v+=1
v=0
# print(contents)
def __str__(self):
a = str(self.contents)
return a
def draw(self,number):
if number <= len(self.contents):
for n in range(number):
n = random.randint(0,len(self.contents)-1)
self.drawn.append(self.contents.pop(n))
# print(len(self.contents))
return self.drawn
else:
for n in range(len(self.drawn)):
self.contents.append(self.drawn.pop(0))
return self.drawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
prob = 0
# print("hat is ",hat)
M = 0
expected = []
v=0
for key,value in expected_balls.items():
while v < value:
expected.append(key)
v+=1
v=0
for n in range(num_experiments):
drawn = []
myhat = hat
drawn = myhat.draw(num_balls_drawn)
sdrawn = sorted(drawn)
# print(sdrawn)
sexpected = []
sexpected = sorted(expected)
# print(sexpected)
# print(M)
for s in expected:
if s in sdrawn:
sdrawn.remove(s)
sexpected.remove(s)
# print(sdrawn)
# sdrawn.
if sexpected == []:
M+=1
#essa linha zera drawn e myhat
myhat.draw(len(hat.contents)+1)
prob = M / (num_experiments)
return prob
Not sure if this deserves a new post