Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

I have run my code in sublime and each time I run it I get a different probability (between 0.38 and 0.41).
I am using the ‘hat = Hat(black=6, red=4, green=3)’ case in the description to run my tests
I can pass tests 1-3 but can’t pass test 4
I have tried clicking the ‘run the tests’ button a few times in case there was a chance occurrence of the same probability

Your code so far

import copy
import random

class Hat:

    def __init__(self, **kwargs):
        if not kwargs:
            print("no values passed")
        else:
            self.contents = [
                key
                for key, value in kwargs.items()
                for value in range(value)
            ]

    def draw(self, draw_count):
        ball_list = []
        
        num_to_draw = 0
        if draw_count > len(self.contents):
            num_to_draw = len(self.contents)
        else:
            num_to_draw = draw_count

        while num_to_draw > 0:
            random_ball = round(random.random() * (len(self.contents) - 1))
            ball_list.append(self.contents[random_ball])
            self.contents.pop(random_ball)
            num_to_draw -= 1

        return ball_list

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    good_draws = 0
    experiments = num_experiments

    expected_list = [
        key
        for key, value in expected_balls.items()
        for value in range(value)
    ]

    while num_experiments > 0:
        num_experiments -= 1
        hat_copy = copy.deepcopy(hat)
        draw_list = hat_copy.draw(num_balls_drawn)

        # see if all the expected_list are in draw_list
        list_count = len(expected_list)

        for i in expected_list:
            if i in draw_list:
                draw_list.remove(i)
                list_count -= 1
            if list_count == 0:
                good_draws += 1

    return good_draws / experiments

Your browser information:

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

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

1 Like

So after going through the code, it seems there are a few key fixes needed to pass Test 4:

  1. Ball Drawing Logic: The current implementation uses random.random() with round(), which introduces bias and does not result in a uniform distribution of outcomes.
    Fix: Use random.sample() to draw balls without replacement. This ensures an unbiased and uniform selection. Then, use a for loop to remove the drawn balls from the hat’s contents.

  2. Validating Expected Balls: Currently, the code manually checks and removes expected balls from the drawn list. This approach can fail in edge cases when removing duplicates like this. For example, if the required number of a specific ball isn’t met, the code might still incorrectly count it as a match.
    Fix: Use collections.Counter to count the occurrences of each color in the drawn balls. Then, check if all the required ball colors are present in the draw in at least the expected quantities by comparing the actual counts (draw_counter) to the expected counts (expected_balls). If all conditions are met, the draw is considered successful.

Let us know if this works or if you need further help.

Hi,
Thank you for the quick response.
After implementing the first fix you suggested (using random.sample()) it worked.
I will still update my code and look into your second point as I really enjoy and appreciate the mention of extra methods.
Thank you ! :slight_smile:

1 Like

No problem. Happy to help!