Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Need help
Question the result of the 3rd test failing. I placed debug print statements to see what’s happening.
Hat is create using list of 19 slots. The number of balls passed is 20.
FAIL: test_hat_draw_2 (test_module.UnitTests.test_hat_draw_2)

AssertionError: 19 != 0 : Expected hat draw to leave no items in contents.
self.assertEqual(actual, expected, ‘Expected hat draw to leave no items in contents.’)
I have a if statement comparing where the number of balls == len of contents.
instruction:
“If the number of balls to draw exceeds the available quantity, return all the balls.”
The way I read the instructions
case 1: 20 > 19 return the entire contents.
case 2: 20 == 20 then return empty list.
Any clue what’s happening here?

I placed debug print statements:

*****number of balls 20 to extract is bigger than the number of balls in the hat 19
***** contents [‘yellow’, ‘yellow’, ‘yellow’, ‘yellow’, ‘yellow’, ‘red’, ‘green’, ‘green’, ‘green’, ‘blue’, ‘blue’, ‘blue’, ‘blue’, ‘blue’, ‘blue’, ‘blue’, ‘blue’, ‘blue’, ‘test’]
#balls 20 contents len 19

Your code so far

import copy
import random
import secrets

class Hat(): 

     def __init__(self,**args):
    #if your hat is {'red': 2, 'blue': 1}, contents should be ['red', 'red', 'blue']
        self.args = args
        self.contents =[] 
        for x,y in args.items():
            self.contents.extend([x] * y) 
                
        self.randon_limit = len(self.contents)
        
     def draw(self, number_of_balls):
        #If the number of balls to draw exceeds the available quantity, return all the balls.
      
        print(f' #balls {number_of_balls}  contents len {len(self.contents)}')
        if number_of_balls > len(self.contents):
            return self.contents
        elif number_of_balls == len(self.contents):
            self.contents = []
            return self.contents
        
        returned_balls = []
        contents = self.contents #.copy() 
         # random.sample() is an built-in function of random module in Python that
         #returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.
        #drawn_balls = []
        #while len(drawn_balls) != number_of_balls:
        #    ball = random.choice(self.contents)
        #    self.contents.remove(ball)
        #    drawn_balls.append(ball)
        drawn_balls = random.sample(self.contents,number_of_balls)  
        for ball in drawn_balls:
            self.contents.remove(ball)             
        
        return drawn_balls

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    n = 0
    m = int(num_experiments)
    print(f'start {hat.args} expected: {expected_balls}, num: {num_balls_drawn}, iterations:{num_experiments}')
    for _ in range(num_experiments):
        
           # Create a copy of the hat to avoid altering the original
        hat_copy = copy.deepcopy(hat) 
        ret_list = hat_copy.draw(num_balls_drawn)
        
        
        returned_balls = {x:ret_list.count(x) for x in ret_list}
        print(f'**returned  {returned_balls}')
        hit = True
       
        #if len(returned_balls) == len(expected_balls):
        for color, number in expected_balls.items() :
            if returned_balls.get(color, -1) != number:
                hit = False
                break
         
       
        if hit:
            print(f' HIT from call:  = expected: {expected_balls}') 
            n += 1
     
    if hit:
        n += 1
    probability =  n / num_experiments
    print('&**$$$\n', n, '/' , num_experiments, '=',  probability, '\n&**$$$\n' )
    return probability


hat = Hat(blue =  3, red =  2, green =  6) 
probability = experiment(hat=hat,
                 expected_balls={'blue': 2, 'green': 1} ,
               num_balls_drawn=4,
               num_experiments=1000)
  

print(probability)











Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

what you need to do is return the whole content of the hat without changing the order, but also empty the hat (self.contents need to be empty)

you need to do both things to pass the test

yes, thanks.
Not sure but I guess the assumption in the instructions would be to first remove and then test. That action would have empty the contents.
Where I was testing the condition and returning the contents.
Which goes to my original confusion. Each experiment stats with the original hat. Why the instructions to remove the items from contents? Waste of cycles.

To have uniformity of behaviour I believe

If you have feedback you can open an issue on github if you want