Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pearls in the Box by juestr
def checkio(marbles: str, step: int) -> float:
n = len(marbles)
# (whites in box, probability of box) [blacks are implied]
states = [(marbles.count('w'), 1.0)]
for _ in range(step - 1):
# create all new possible states for taking a white or black marble
states = \
[(w-1, p*w/n) for (w, p) in states if w > 0] + \
[(w+1, p*(n-w)/n) for (w, p) in states if w < n]
# for the last probability we can just sum over independent states
pw = round(sum(w*p for (w, p) in states) / n, 2)
return pw
April 11, 2019