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 sino_arr
def checkio(marbles,step):
N = len(marbles)
Nw0 = marbles.count("w")
def rec_formula(Nw,n):
if(n == 1):
return(int(Nw0 == Nw))
elif(Nw == 0):
P = rec_formula(1,n-1)*(1/N)
elif(Nw == N):
P = rec_formula(N-1,n-1)*(1/N)
else:
P = rec_formula(Nw-1,n-1)*(N-Nw+1)/(N) + rec_formula(Nw+1,n-1)*(Nw+1)/(N)
return P
E = 0
for i in range(N+1):
E += rec_formula(i,step)*(i/N)
return(round(E,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!")
Aug. 21, 2018