Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:
The probability calculator is resulting 0.061. From what I read on the guide:

  • I will randomize values from the class’s hats and remove the randomized values from the hats (I deep copied the contents of the hat class and removed one by one the values of the randomized from the copy). This loop will go on until the specified N, and if the hat’s length becomes less than the M I will reset hat’s content as it was so it would start over.
  • Whenever the copied content becomes the same as the expected, I increase the count by 1.

I’m doing this, it’s counting 61 and when it divides by 1000 it’s becoming 0.061. But I know that’s incorrect, and I just couldn’t find an answer. Maybe I understood it wrong from the guide? Lmk, thanks in advance

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Could you explain how you are checking if expected balls are among the drawn balls?

Expected balls among the drawn balls? I’m checking expected balls among the balls that the hat class already has after the drawn balls (randomly) are removed. I’m supposed to check if the expected balls match the drawn ones?
But if I check it according to the random values, it might not result anything. the num_balls_drawn is 4, the expected has 3 (2 blue, 1 green), so whatever I randomize it will give 4 random values from the contents of the hat class, which is more than the expected. I think I’m missing something here

To write out one of the tests:

  • Each experiment should start with 11 balls in hat: "blue": 3, "red": 2, "green": 6
  • In each experiment 4 balls are drawn from hat.
  • Among those 4 drawn it is expected to have: "blue":2, "green":1. If that’s true, experiment should be counted as success.

If you worry about randomness of it, test is seeding the random module with specific number, making the results reproducible.

I can’t seem to find a way to check if the expected is in the 4 drawn. Can you suggest any method?

There’s couple ways. One could be, for each ball color and count from the expected ball, checking (counting) against the drawn balls, if number of balls in specific color is at least as expected.

I went with this way:
randomized is a list, like [green, green, yellow, blue, blue, blue…] with “” of course
and the expected_balls is the normal dictionary passed with the function

    nonfound_trigger = False
    
    for color,count_value in expected_balls.items():
      if color in randomized and nonfound_trigger == False:
        count_random = 0
        for i in randomized:
          if i == color:
            count_random += 1
            print(color,i,count_random)

        if count_value > count_random:
          nonfound_trigger = True
          print(color,i,count_random, "trggered true")
          continue
      else:
        nonfound_trigger = True
        print("triggered true b2")

    if nonfound_trigger == False:
      caught_expected += 1


    count += 1
    print(count)

But this is resulting 267, can you check if there’s an error here please? also this strange error is being popped up:

Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/test_module.py", line 29, in test_prob_experiment
    probability = prob_calculator.experiment(hat=hat, expected_balls={"yellow":2,"blue":3,"test":1}, num_balls_drawn=20, num_experiments=100)
  File "/home/runner/boilerplate-probability-calculator/prob_calculator.py", line 48, in experiment
    **randomized = random.sample(copied_contents, num_balls_drawn)**
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/random.py", line 482, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative

UPDATE: just noticed that the traceback is for a different issue. This:
probability = prob_calculator.experiment(hat=hat, expected_balls={"yellow":2,"blue":3,"test":1}, num_balls_drawn=20, num_experiments=100)
is weird, because hat has 19 balls, and the drawn is greater than the num_balls_drawn. Whenever num_balls_drawn is greater than 19, we put a condition to return 1.0?

Thank you. Solved it,

If the number of balls to draw exceeds the available quantity, return all the balls.

1 Like

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