Scientific Computing with Python - Probability Calculator - could there be a mistake in the test?

Hi! I cannot get through test 3 (line 29 in the test_module) because of error: Sample larger than population or is negative. I assume that the error is because in the test task, the number of the balls to draw (20) is higher than total initial number of balls in the hat (18). So there is not enough balls even before the draw, nothing to return to hat. But there is no expected return in that case, so I end up with error message in console. My code does return all the balls to hat if the there is not enough for the next draw.
[boilerplate-probability-calculator - Replit]

If this is an accurate description of your code, then it’s out of spec. The spec requires draw() to return all balls in the hat if more are requested than are available in the hat. There are no known errors in these tests.

Yes, but in the task (line 29 of the test module), there is not enough balls to make even the first draw. I am citing here the task:

hat = prob_calculator.Hat(yellow=5,red=1,green=3,blue=9,test=1)
probability = prob_calculator.experiment(hat=hat, expected_balls={“yellow”:2,“blue”:3,“test”:1}, num_balls_drawn=20, num_experiments=100)

So there is nothing to return to hat, because the first draw was impossible to do - num_balls_drawn > total (initial) number of balls in the hat.

The Hat class should have a draw method that accepts an argument indicating the number of balls to draw from the hat. This method should remove balls at random from contents and return those balls as a list of strings. The balls should not go back into the hat during the draw, similar to an urn experiment without replacement. If the number of balls to draw exceeds the available quantity, return all the balls.


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

There is a lot of text to read in this project unfortunately, and its all important.

You are calling

    self.random_list = random.sample(self.contents, self.number) #creates a list of a given number of random values

instead of returning everything like the project wants, but that function call fails because the number of items you are trying to sample is too high.

You shouldn’t use that line anyways. You’re going to need to simplify your draw method to get it to work with the tests.

OK. I see the spec to return the balls, but isn’t it after they have been drawn, and there is not enought in the hat to make the next draw ? But we have a situation in the task, where in the very beginning, before the draw, there is not enough balls to make the first draw.

hat: yellow=5,red=1,green=3,blue=9,test=1 ------this equals to 19 balls
num_balls_drawn=20 ------ this asks to draw 20 balls, which results in error when trying to do, because there is not enough balls in the hat.

My code returns the balls back to hat when there is not enough by “restoring” the contents list to the initial (before any draws). So in case when the total number of balls in the hat before the experiment is less than number of balls to draw, there is unavoidably an error unless you add more balls to the hat than there were before the experiment, so it’s not an original hat anymore

Working backwards from your error message,

It is impossible to call random.sample if you are requesting more items than the list contains.

Therefore, you cannot cannot random.sample if the number of items requested is greater than the total number of items in the hat.

The order of your code matters here. You are blocking the return of all balls by calling random.sample and generating an error.

Your code is demanding that random.sample works before you check if it can work. That won’t work. You need to avoid the error by not calling random.sample if its impossible for that method to work.

Or, like I said, you can avoid calling random.sample at all because it still won’t play nice with the test suite once you fix this bug in your logic.

Yes, I see that I need to do the check before random.sample. But in specs there is no mentioning what the return should be in that case, and in the test module there is no expected return to that

Per the spec:

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

As mentioned before, there is nothing to return. Total number of balls in the hat is lower than the number of balls to draw. There wasn’t a single draw to return from, because the task asks to draw 20 balls from the hat that only has 19 balls before any draw was done

You are overthinking it.


IF: the number of balls requested is larger than the number of balls in the hat
THEN: return absolutely everything that is currently inside of the hat
ELSE: insert drawing a subset of balls logic here

So, if 20 balls are requested but you only have 19, you only return 19 balls.

That’s it. Like I said, don’t use random.sample at all. It’s not needed and its just confusing you.

Aaaah, I finally see what you /spec mean by return all the balls. To return all available balls. Thanks! The “return 19 balls” line helped a lot

1 Like

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