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 mehWincenty
def P(w, b, n):
if n > 0 and w > 0 and b > 0:
return w/(w+b)*P(w-1, b+1, n-1)+b/(w+b)*P(w+1, b-1, n-1)
elif n > 0 and w == 0:
return P(1, b-1, n-1)
elif n > 0 and b == 0:
return P(w-1, 1, n-1)
else :
return w/(w+b)
def checkio(marbles, step):
w = marbles.count("w")
b = marbles.count("b")
return round(P(w, b, step-1), 2)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio('bbw', 3) == 0.48, "1st example"
assert checkio('wwb', 3) == 0.52, "2nd example"
assert checkio('www', 3) == 0.56, "3rd example"
assert checkio('bbbb', 1) == 0, "4th example"
assert checkio('wwbb', 4) == 0.5, "5th example"
assert checkio('bwbwbwb', 5) == 0.48, "6th example"
Nov. 11, 2016