Tell us what’s happening:
Cannot get tests 3 and 4 to pass /draw method: I am deleting the drawn items from contents in any case, while outputting the drawn list. Still tells me draw method should behave correctly for drawn balls > balls in hat/ experiment function: returns the probability that seems right during my experiments (1 if drawing all balls), still browser console states ‘0.0 != 1.0 with delta 0.1’ and test 4 expects a different probability. I tried multiple solutions (some working in forum) with no luck
Your code so far
import copy
import random
class Hat:
def __init__(self, **kwargs):
keyList = list(kwargs.keys())
valueList = list(kwargs.values())
self.contents = []
for amount in valueList:
index = valueList.index(amount)
for i in range(amount):
self.contents.append(keyList[index])
def draw(self, ballsDrawn):
if ballsDrawn >= len(self.contents):
drawResult = self.contents.copy()
self.contents.clear()
else:
drawResult = random.sample(self.contents, ballsDrawn)
for i in drawResult:
self.contents.remove(i)
return drawResult
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0
expectedList = []
for k,v in expected_balls.items():
k = [k] * v
expectedList.extend(k)
for _ in range(num_experiments):
hatCopy = copy.deepcopy(hat)
drawList = hatCopy.draw(num_balls_drawn)
if compare_lists(drawList, expectedList):
print('good ---- e:', expectedList, 'd:', drawList)
M += 1
else:
print('bad ---- e:', expectedList, 'd:', drawList)
return M/num_experiments
def compare_lists(list1, list2):
for item in set(list2):
if list1.count(item) < list2.count(item):
return False
return True
hat1 = Hat(yellow=3, blue=2, green=6)
hat2 = Hat(red=5, orange=4)
hat3 = Hat(red=5, orange=4, black=1, blue=0, pink=2, striped=9)
hat4 = Hat(black=6, red=4, green=3)
print(experiment(hat4, {'red':2, 'green':1}, 5, 2000))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project