Hi,
I am struggling to figure out why my code is failing all 3 tests… I couldn’t even have any clue why it fails the 1st one (“contents”)… I tested the code standalone for “hat.contents” and it produces the results as expected but in the test results on replit it shows strange results (additional elements or incorrect number of elements) that I couldn’t figure out why…
This is my code:
class Hat:
contents = list()
def __init__(self, **balls):
# balls are passed in as a dict
self.balls = balls
# convert self.balls from a dict to a list named self.contents
for item in balls:
for i in range(balls.get(item)):
self.contents.append(item)
def draw(self, num2draw):
if num2draw >= len(self.contents):
return self.contents
else:
ballsDrawn = random.sample(list(enumerate(self.contents)), num2draw)
newContents = list()
ballsDrawn_list = list()
for id, ball in ballsDrawn:
ballsDrawn_list.append(ball)
for id, ball in list(enumerate(self.contents)):
if not (id, ball) in ballsDrawn:
newContents.append(ball)
self.contents = newContents
return ballsDrawn_list
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
allMatchedCount = 0
num_cycle = num_experiments
contents = copy.deepcopy(hat.contents)
while num_cycle > 0:
hat.contents = copy.deepcopy(contents)
ballsDrawn = hat.draw(num_balls_drawn)
ballsDrawn_dict = dict()
# convert
for i in ballsDrawn:
ballsDrawn_dict[i] = ballsDrawn_dict.get(i, 0) + 1
# Check if expected balls got drawn
elem_matchCount = 0
for ball in expected_balls:
if ball in ballsDrawn_dict and ballsDrawn_dict[ball] >= expected_balls[ball]:
elem_matchCount += 1
if elem_matchCount == len(expected_balls):
allMatchedCount += 1
num_cycle -= 1
return allMatchedCount / num_experiments
This is the FAIL msg for the 1st test on “contents” from replit:
======================================================================
FAIL: test_hat_class_contents (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-probability-calculator/test_module.py", line 10, in test_hat_class_contents
self.assertEqual(actual, expected, 'Expected creation of hat object to add correct contents.')
AssertionError: Lists differ: ['blue', 'blue', 'blue', 'blue', 'red', 're[89 chars]lue'] != ['red', 'red', 'red', 'blue', 'blue']
First differing element 0:
'blue'
'red'
First list contains 12 additional elements.
First extra element 5:
'red'
+ ['red', 'red', 'red', 'blue', 'blue']
- ['blue',
- 'blue',
- 'blue',
- 'blue',
- 'red',
- 'red',
- 'green',
- 'green',
- 'green',
- 'green',
- 'green',
- 'green',
- 'red',
- 'red',
- 'red',
- 'blue',
- 'blue'] : Expected creation of hat object to add correct contents.
FAIL msg for 2nd test:
======================================================================
FAIL: test_hat_draw (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-probability-calculator/test_module.py", line 16, in test_hat_draw
self.assertEqual(actual, expected, 'Expected hat draw to return two random items from hat contents.')
AssertionError: Lists differ: ['red', 'blue'] != ['blue', 'red']
First differing element 0:
'red'
'blue'
- ['red', 'blue']
+ ['blue', 'red'] : Expected hat draw to return two random items from hat contents.
3rd test FAIL msg:
======================================================================
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.249 != 0.272 within 0.01 delta (0.02300000000000002 difference) : Expected experiment method to return a different probability.
----------------------------------------------------------------------