Good afternoon!
I have some problem with test 3. My probability around 0.21 is much lower.
I have written other topics, but I don’t want to copy other code.
Thank you in advance.
Your code so far
import random
from copy import deepcopy
class Hat:
def __init__(self, **kwargs):
self.contents = []
for key, value in kwargs.items():
if value == 1:
self.contents.append(key)
elif value > 1:
self.contents += [key] * value
self.balls = deepcopy(self.contents)
def draw(self, balls_to_draw=0):
draw_out = []
balls_in_the_hat = deepcopy(self.contents)
if balls_to_draw >= len(balls_in_the_hat):
draw_out = deepcopy(balls_in_the_hat)
self.contents = deepcopy(self.balls)
else:
for digit in range(balls_to_draw):
elem = random.choice(balls_in_the_hat)
draw_out.append(elem)
balls_in_the_hat.remove(elem)
self.contents = deepcopy(balls_in_the_hat)
return draw_out
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
match = 0
expected_balls_listi = []
for item, numb in expected_balls.items():
if numb == 1:
expected_balls_listi.append(item)
elif numb > 1:
expected_balls_listi += [item] * numb
for number in range(num_experiments):
drawn_out_balls = deepcopy(hat.draw(num_balls_drawn))
checker = 0
for item in expected_balls_listi:
if item in drawn_out_balls:
checker += 1
drawn_out_balls.remove(item)
if checker == len(expected_balls_listi):
match += 1
return match / num_experiments
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Probability Calculator
Link to the challenge: