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 yuwai
def checkio(marbles: str, step: int) -> float:
total=len(marbles)
w=marbles.count('w')
def f(N,w,total): #利用递归来算概率,参数分别为(操作数,白球数,总球数)
if N==1:
return w/total
return w/total*f(N-1,w-1,total)+(total-w)/total*f(N-1,w+1,total)
return round(f(step,w,total),2)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
print("Example:")
print(checkio('bbw', 3))
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"
print("Coding complete? Click 'Check' to earn cool rewards!")
July 16, 2018