Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Pearls in the Box by Namsoo_Jang
def check_color(marbles):
w,b=0,0
for i in marbles:
if i=='w': w+=1
elif i=='b': b+=1
return w,b
def checkio_modi(w0,b0, step):
pw=w0/(w0+b0)
pb=b0/(w0+b0)
# print("white : ",w0,",black : ",b0,", step :",step, ", pw: ", pw)
if w0<0 or b0<0: return 0
if step==1: return pw
else:
return pw*checkio_modi(w0-1,b0+1,step-1)+pb*checkio_modi(w0+1,b0-1,step-1)
def checkio(marbles, step):
#print(marbles,step)
w0,b0=check_color(marbles)
pw_in_step=checkio_modi(w0,b0,step)
print(pw_in_step)
return(round(pw_in_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"
May 26, 2017