Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
simple recursion solution in Clear category for Pearls in the Box by Leonix
def checkio(marbles: str, step: int) -> float:
return round(prob(marbles.count('w'), marbles.count('b'), step), 2)
def prob(white: int, black: int, step: int) -> float:
if step == 1:
return white / (black + white)
black_prob = 0 if black == 0 else black*prob(white+1, black-1, step-1) / (black+white)
white_prob = 0 if white == 0 else white*prob(white-1, black+1, step-1) / (black+white)
return black_prob + white_prob
July 19, 2019