Probability Calculator Challenge

Problem solved

import copy
import random
# Consider using the modules imported above.

class Hat:
  
  def __init__(self, **kwargs):
    self.contents = []
    for key, value in kwargs.items():
      for num in range(value):
        self.contents.append(key)

  def draw(self, num_balls_to_draw):
    removed_balls = []
    if num_balls_to_draw > len(self.contents):
      return self.contents
    else:
      for i in range(num_balls_to_draw):
        random_index = random.randint(0, len(self.contents) - 1)
        removed_ball = self.contents.pop(random_index)
        removed_balls.append(removed_ball)
      return removed_balls
   
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
  counter = 0
  wanted_draws = []
  
  for key, value in expected_balls.items():
    for num in range(value):
      wanted_draws.append(key)
    
  for num in range(num_experiments):
    draws = copy.deepcopy(hat).draw(num_balls_drawn)
    check = 0

    for key, value in expected_balls.items():
      if draws.count(key) >= value:
        check += 1
        if check >= len(expected_balls):
           counter += 1

  return counter/num_experiments

Notice that probability of the example is 0.0 which also is off. Take another look in the README.md file to see if function follows the expected specification.

I did but i dont know whats wrong

Consider that experiment function is supposed to determine of drawing expected_balls within num_balls_drawn balls from the hat in the num_experiment experiments. Each of the experiments should begin with hat having the same balls.

Take a look also what happens when all balls are drawn. Per specification in readme file once last ball is drawn then all balls should be returned to the hat.

" Consider that experiment function is supposed to determine of drawing expected_balls within num_balls_drawn balls from the hat in the num_experiment experiments. Each of the experiments should begin with hat having the same balls."
I tried to do this with copy_hat = copy.copy(hat) and then
after the start of each experiment:
hat = copy_hat. Why isnt this working?
And at which point do they state that the balls should be returned?

nvm I found the problem.

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