Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple recursive and lru_cache solution in Clear category for Pearls in the Box by tokyoamado
from fractions import Fraction
from functools import lru_cache
def checkio(marbles: str, step: int) -> float:
@lru_cache
def prob_white(marbles: str, step: int) -> Fraction:
p_white = Fraction(marbles.count('w'), len(marbles))
if step == 1:
return p_white
else:
return (
p_white * prob_white(marbles.replace('w', 'b', 1), step - 1) +
(1 - p_white) * prob_white(marbles.replace('b', 'w', 1), step - 1))
return float(round(prob_white(marbles, step), 2))
Nov. 25, 2019
Comments: