Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursive solution in Clear category for Pearls in the Box by Vasily__Chibilyaev
def make_move(b, w, step):
if step==1:
return w/(w+b)
else:
p1=0 if b==0 else make_move(b-1, w+1, step-1)
p2=0 if w==0 else make_move(b+1, w-1, step-1)
return b/(b+w)*p1+w/(b+w)*p2
def checkio(m, step):
return round(make_move(m.count('b'), m.count('w'), step), 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"
Sept. 29, 2017