ProbabityCalc submission

Hello,
I am trying to submit my solution for the Probability_Calculator problem for the past 3 hours but i’ve come to a dead end, since for example on some tests, the method: hat.draw(2) is used and [“blue”, “red”] is expected, but actual is not only that since drawn balls are random, so sometimes we would have something like [“blue”, “blue”] or [“red”, “red”] etc. So, how can my code pass the cases, since on each execution we have different results, but grader demands a specific one.

Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.73

Challenge: Probability Calculator

Link to the challenge:

The test suite picks the seed value for the pseudorandom number generator so that the results are fixed.

It appears that if i also import the seed in my code it prints the fixed results wanted by the grader, but by submitting it without the seed (as suggested) it doesn’t return fixed results.

The seed should already be part of the test suite. You shouldn’t touch the test suite.

I only wrote the program in probability_calculator.py. However, i watched the structure of main.py and in the beggining it has the line random.seed(95), which if i also import on my text editor (not on replit) prints the expected results.

Hello, there are 2 test cases which supposedly are not correct, but they return correct results while running locally. Is there any chance you could help with the submission?

Can you share your code? I can’t guess at the issue without seeing the code.

Sure, but won’t there be a problem if i post it here publicly (it’s my first post so I don’t know how it works).

I see no reason why sharing your code would be a problem.

It would be easiest if you share the link to your repl.

A few things jump out at me.

  def draw(self, balls):
      if(balls > len(self.contents)):
          for content in self.contents:
              self.removed.append(content)
          self.contents = []
      else:
          for i in range(balls):
              to_be_removed = random.choice(self.contents)
              self.removed.append(to_be_removed)
              self.contents.remove(to_be_removed)
      return self.removed

This is not how you were told to reset the contents of 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.

fake_hat.contents.append(drawn_ball)             #insert them so they can be re-removed in next loop

Reaching into the private data members of an object is a huge red flag. Do not do this. Instead you need to draw from a fresh copy of the hat each time.

Regarding the markdown paragraph, I had decided to draw balls one-by-one since that’s what I thought would give some meaning to this phrase: “The balls should not go back into the hat during the draw, similar to an urn experiment without replacement.” However, after you suggestion I changed the draw method by using the random.sample() method and removing the “removed” instance variable. In addition to that, I create a new hat object in every loop, but I still have problems in the submission.

Ok, this is a problem

class Hat:

  contents = []
  removed = []

You are having trouble with class variables vs instance variables.
https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables

Thank you very very much, I was having a really hard time figuring out where the problem was.
(Should I now close this thread or something?)

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