Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Pearls in the Box by aquazul
import math
def c(n, k):
return math.factorial(n) / math.factorial(k) / math.factorial(n - k)
def checkio(marbles, step):
s = step - 1
w = marbles.count('w')
b = marbles.count('b')
n = w + b
pc = 1/n
p = sum(c(s, k)*pc**k*(1-pc)**(s-k) for k in range(0, s+1, 2)) * w / n
p += sum(c(s, k)*pc**k*(1-pc)**(s-k) for k in range(1, s+1, 2)) * b / n
return round(p, 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"
Jan. 20, 2017