Probability Calculator: Cannot pass test 3

I’ve been working on this challenge and it seems that I have completed it but I cannot pass Test #3. When I run the code it gives me a probability very close to the example output.

Is it possible that it is not passing because I am using from collections import Counter import Counter?

Thank you for the help!

import copy
import random
from collections import Counter

class Hat:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
        self.contents = list()
        self.drawn_balls = list()

        for key, value in kwargs.items():
            for _ in range(0, value):
                self.contents.append(key)

    def draw(self, number):
        list_to_remove = copy.copy(self.contents)
        for _ in range(0, number):
            removed_element = random.choice(list_to_remove)
            list_to_remove.remove(removed_element)
            self.drawn_balls.append(removed_element)
        return self.drawn_balls

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    M = 0
    for i in range(0, num_experiments):
        # Draw balls and place into list
        ball_drawn = hat.draw(num_balls_drawn)
        # Convert expected_balls into same format as ball_drawn
        target_balls = list()
        for key, value in expected_balls.items():
            for _ in range(0, value):
                target_balls.append(key)
        # Determine if the expected_balls are in the draw_balls list
        if not Counter(target_balls) - Counter(ball_drawn):
            M += 1
        ball_drawn.clear()
    # Calculate probability
    probability = round(M/num_experiments, 3)
    return probability

Naah. Take another look at the draw method requirements. Specifically what should happen when wanted is higher number of balls than it is in the hat.

Ahhhh thank you! Got to read it more carefully next time

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.