Hello guys,
I have written code for the probability calculator. As of now, one test case fails i.e.
python main.py
Probability: 0.0006666666666666666
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-probability-calculator/test_module.py", line 26, in test_prob_experiment
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.002 != 0.272 within 0.01 delta (0.27 difference) : Expected experiment method to return a different probability.
----------------------------------------------------------------------
Ran 3 tests in 0.003s
FAILED (failures=1)
Here is my code
import copy
import random
# Consider using the modules imported above.
class Hat:
def __init__(self,**kwargs):
self.contents_dict = {}
self.contents = []
# self.contents_copy=copy.deepcopy(self.contents)
for key,value in kwargs.items():
for i in range(0,value):
self.contents.append(key)
def draw(self,number_of_balls):
drawn_items=[]
if number_of_balls>len(self.contents):
return self.contents
else:
contents_copy=copy.deepcopy(self.contents)
x = random.sample(range(len(self.contents)), number_of_balls)
for i in x:
drawn_items.append(contents_copy[i])
self.contents.remove(contents_copy[i])
return drawn_items
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
correct_results = 0
contents=[]
contents_copy = copy.deepcopy(hat.contents)
for key,value in expected_balls.items():
for i in range(0,value):
contents.append(key)
# print(f"contents:{contents}")
for i in range(0, num_experiments):
hat.contents = contents_copy
drawn_balls = hat.draw(num_balls_drawn)
# print(f'drawn_balls:{drawn_balls}')
if all(items in drawn_balls for items in contents):
correct_results+=1
# print(correct_results)
return (correct_results/num_experiments)
Tried various things but not sure where I am going wrong, can someone please help. Also, any suggestions to improve existing code will be helpful