Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Binomial expansion solution in Clear category for Pearls in the Box by abc512
def checkio(marbles, step):
pn, pw = 1/len(marbles), marbles.count('w')/len(marbles)
# p = pw*C(s-1,0)*pn**0*(1-pn)**(s-1) + (1-pw)*C(s-1,1)*pn**1*(1-pn)**(s-2) + ...
# 1-p = (1-pw)*C(s-1,0)*pn**0*(1-pn)**(s-1) + pw*C(s-1,1)*pn**1*(1-pn)**(s-2) + ...
# 2*p-1 = (2*pw-1)*C(s-1,0)*pn**0*(1-pn)**(s-1) + (-2*pw+1)*C(s-1,1)*pn**1*(1-pn)**(s-2) + ...
# 2*p-1 = (2*pw-1)*(C(s-1,0)*(-pn)**0*(1-pn)**(s-1) + C(s-1,1)*(-pn)**1*(1-pn)**(s-2) + ...
# 2*p-1 = (2*pw-1)*(1-2*pn)**(s-1)
# p = (pw-1/2)*(1-2*pn)**(s-1) + 1/2
return round((pw-1/2)*(1-2*pn)**(step-1) + 1/2, 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"
March 3, 2018