Tell us what’s happening:
I am having a hard time understanding the instructions.
" The balls should not go back into the hat during the draw
If the number of balls to draw exceeds the available quantity, return all the balls."
Example Hat(black = 6, red = 4, green = 3)
expected_balls={‘red’: 2, ‘green’: 1} ,
num_balls_drawn=5,
num_experiments=20)
the number of ball to draw is 5, Then the function will return a list 5 items until the list has less than 5 items. Then the remainder of the list is always returned. The original list is 13. After removing 5 per drawing , the remaining calls will return a list of 3 items.
So how can you ever expect the function to return 3 items ({‘red’: 2, ‘green’: 1} ?
The other question is that the removal is random. Yet the test is expecting the same result {red, blue} ? how is this random?
Your code so far
import copy
import random
class Hat():
def draw(self, number_of_balls):
#If the number of balls to draw exceeds the available quantity, return all the balls.
if number_of_balls > len(self.contents):
return self.contents
returned_balls = []
contents = self.contents #.copy()
#iterate to remove the number of balls
i = number_of_balls
while i:
index = random.randint(0, len(self.contents))
#print(f'find {index} remove: {contents}')
if len(self.contents) > index :
#print(f'find {index} remove: {self.contents[index]}')
item = self.contents[index]
print(f'find {index} remove: {item}')
returned_balls.append(item )
self.contents.remove(item)
i -= 1
#else:
# print (f'iternation: {i} {index} not found')
print (f'returned: {returned_balls} contents: {self.contents}')
return returned_balls
def __init__(self,**args):
#if your hat is {'red': 2, 'blue': 1}, contents should be ['red', 'red', 'blue']
self.args = args
self.contents =[]
for x,y in args.items():
for i in range(y):
self.contents.append(x)
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
n = 0
m = int(num_experiments)
print(f'start {hat.args} expected: {expected_balls}, num: {num_balls_drawn}, iterations:{num_experiments}')
for _ in range(m):
ret_list = hat.draw(num_balls_drawn)
#ret_balls = list of balls
returned_dict = {x:ret_list.count(x) for x in ret_list}
shared_items = {k: returned_dict[k] for k in returned_dict \
if k in expected_balls and returned_dict[k] == expected_balls[k]}
#print(f' shared {len(shared_items)} contents: {hat.contents}')
if len(shared_items) :
print(f' {len(shared_items)} shared: {shared_items}')
if len(shared_items) == len(expected_balls):
n += 1
if n:
return m / n
return 0
#for x,y in expected_balls.items():
# for i in range(y):
#E expected_ball_list.append(x)
#[x for x in a if x in b]
#list(set(a).intersection(set(b)))
# new_list = []
#for element in a:
# if element in b:
# new_list.append(element)
# expected_balls = dictionary {'red':2,'green':1}
#des the list containt 2 occurances of 'red' and 1 occence of green
#hat = Hat(red=5, orange=4, black=1, blue=0, pink=2, striped=9)
hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat,
expected_balls={'red':2,'green':1},
num_balls_drawn=5,
num_experiments=20)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project