Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
My First, Recursive solution in Speedy category for Pearls in the Box by d_rabenko
from decimal import *
def prob(marbles, step):
p_w = marbles.count('w') / len(marbles)
if step == 1:
return p_w
else:
return (1 - p_w) * prob(marbles.replace('b', 'w', 1), step - 1) + \
p_w * prob(marbles.replace('w', 'b', 1), step - 1)
def checkio(marbles, step):
temp = Decimal(prob(marbles, step) * 100).quantize(Decimal()) / 100
return float(temp)
#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"
Oct. 9, 2017
Comments: