March 12th, 2019

codingdirectional: Return the probability of drawing a blue marble

Python, by admin.

It seems like I have not decide yet which project am I going to create next so why not just work on another python solution on CodeWars in this chapter. I think I will work on a few more python questions in the next few chapters before starting a brand new python project.

The python question I am about to solve goes like this.

You and a friend have decided to play a game to drill your statistical intuitions. The game works like this:

You have a bunch of red and blue marbles. To start the game you grab a
handful of marbles of each color and put them into the bag, keeping
track of how many of each color go in. You take turns reaching into the
bag, guessing a color, and then pulling one marble out. You get a point
if you guessed correctly. The trick is you only have three seconds to
make your guess, so you have to think quickly.

You’ve decided to write a function, guessBlue() to help automatically calculate whether you should guess “blue” or “red”. The function should take four arguments:

  • the number of blue marbles you put in the bag to start
  • the number of red marbles you put in the bag to start
  • the number of blue marbles pulled out so far, and
  • the number of red marbles pulled out so far.

guessBlue() should return the probability of drawing a blue marble, expressed as a float. For example, guessBlue(5, 5, 2, 3) should return 0.6.

This question above actually needs us to think a few more times before we find out the solution but it has been put under the 8 kyu question, I think it should be at least 7 or 6 kyu question instead.

Before we write out the code let us solve the problem first by hand.

We need to get the quantity of the remaining blue marbles in the bag so

blue_remained = blue_start - blue_pulled

Same goes to red marbles

red_remained = red_start - red_pulled

Finally the probability of drawing a blue marble from the bag will become

blue_remained/(blue_remained + red_remained)

Here is the entire method.

def guess_blue(blue_start, red_start, blue_pulled, red_pulled):
    blue_remained = blue_start - blue_pulled
    red_remained = red_start - red_pulled
    return blue_remained/(blue_remained + red_remained)

The solution above is not complex but you need to think a little bit to find that solution.

Back Top

Comments are closed.