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 yarmak_vladislav
# migrated from python 2.7
def checkio(marbles, step):
def solve(white, black, step):
if step==1:
return float(white)/(white+black)
if black and white:
return float(white)/(white+black)*solve(white-1, black+1, step-1)+float(black)/(white+black)*solve(white+1, black-1, step-1)
elif not white:
return solve(white+1, black-1, step-1)
else:
return solve(white-1, black+1, step-1)
return round(solve(marbles.count('w'), marbles.count('b'), step)*100)/100.0
#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"
June 14, 2014