Probability Calculator-Why does self.contents need to be changed on each call to draw()?

  • The balls should not go back into the hat during the draw, similar to an urn experiment without replacement.

The above is the line in the project readme relevant to my question.

Whether self.contents has the drawnBalls removed in draw() or not, the probability calculation remains the same. What is the point of this requirement?

  class Hat:
  def __init__(self, **kwargs):
    self.contents = []
    self.copy = []

    for color, n in kwargs.items():
        #self.contents+=[color] * n
        self.copy+=[color] * n
        self.contents = self.copy.copy()

  def draw(self, n):
    self.contents = self.copy.copy()
    if n > len(self.contents): return self.contents
    drawn = random.sample(self.contents,k=n)
    for color in drawn:
    return drawn

  def experiment(**kwargs):

   (hat, expected_balls, num_balls_drawn, num_experiments) = kwargs.values()

   M = 0
   i = num_experiments
   while i > 0:
    drawnBalls = hat.draw(num_balls_drawn)
    drawnBallsDict = dict()

    for color, n in expected_balls.items():
        if color in drawnBalls and drawnBalls.count(color) >= n:
            drawnBallsDict[color] =  n
    if drawnBallsDict == expected_balls: M+=1
  return M/num_experiments

It may be the same for some cases, but there are others in which it definitely will be different. Consider for example hat with 4 different color balls and drawing 4 balls from it. Without the replacement in the end every ball color will be drawn once. If ball is returned after each draw to the hat, then it’s much less likely to draw every color again.

1 Like

I agree with you. I select the balls from the hat with the random.sample() method, which does already select without repetition (which was the requirement in the instructions).

I will need to add a copy of contents as you show above, since I am not passing the test that check the length of content after the draw. The error displayed is:
Expected hat draw to reduce number of items in contents.
My opinion is that such part of the test is not really in line with the requirements, as they only talk about repetition and not about actual updates to the Hat object.

This is my replit:

For this particular probability calculation to match the requirements, you need to empty the hat as you draw from it. If you replace the balls after drawing, that is a completely different probability problem.

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