Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 1.42 KB

8 kyu - Thinkful - Number Drills: Blue and red marbles.md

File metadata and controls

32 lines (27 loc) · 1.42 KB

Task

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, guess_blue() 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. guess_blue() should return the probability of drawing a blue marble, expressed as a float. For example, guess_blue(5, 5, 2, 3) should return 0.6.

My solution

def guess_blue(blue_start, red_start, blue_pulled, red_pulled)
  ((blue_start - blue_pulled).to_f / (blue_start + red_start - blue_pulled - red_pulled).to_f).to_f
end

Better solution

def guess_blue(blue_start, red_start, blue_pulled, red_pulled)
  red_prob = (red_start - red_pulled) 
  blue_prob =(blue_start - blue_pulled)
  prob = blue_prob / (red_prob + blue_prob).to_f
end