Tell us what’s happening:
It is ok that the test fail and let me know that the experiment method should return a different probability.
First question: What is that probability to acheive (and its tolerance to be considered a valid result)?
Second question: Why there is no specification of which tests are gonna be run to validate the code as there is in other exercises? I feel this could be a better and helpful way to approach and debug the solution.
Your code so far
import copy
import random
class Hat:
def __init__(self, **kwargs):
self.contents=[]
for key, value in kwargs.items():
for i in range(value):
self.contents.append(key)
def draw(self, number):
all_removed = []
if number > len(self.contents):
return self.contents
for i in range(number):
removed = self.contents.pop(int(random.random() * len(self.contents)))
all_removed.append(removed)
return all_removed
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
count = 0
for i in range(num_experiments):
expected_copy = copy.deepcopy(expected_balls)
hat_copy = copy.deepcopy(hat)
colors_gotten = hat_copy.draw(num_balls_drawn)
for color in colors_gotten:
if color in expected_copy:
expected_copy[color]-=1
if(all(x <= 0 for x in expected_copy.values())):
count +=1
return count / num_experiments
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Challenge Information:
Scientific Computing with Python Projects - Probability Calculator