def chance(flips=12, russels=8, target=1, remaining=40):
if target == 0: return 1
elif flips == 0: return 0
else: return (
russels/remaining * chance(flips-1, russels-1, target-1, remaining-1) +
(remaining-russels)/remaining * chance(flips-1, russels, target, remaining-1)
)
def pp(x):
if x < 0.1: return "{:.2%}".format(x)
else: return "{:.1%}".format(x)
print("flips/russels| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8")
print("-------------+-------+-------+-------+-------+-------+-------+-------+-------")
for i in range(1,13):
print("{:2d} | {} | {} | {} | {} | {} | {} | {} | {}".format(
i,
pp(chance(i, target=1)),
pp(chance(i, target=2)),
pp(chance(i, target=3)),
pp(chance(i, target=4)),
pp(chance(i, target=5)),
pp(chance(i, target=6)),
pp(chance(i, target=7)),
pp(chance(i, target=8))
))
ZGVmIGNoYW5jZShmbGlwcz0xMiwgcnVzc2Vscz04LCB0YXJnZXQ9MSwgcmVtYWluaW5nPTQwKToKCWlmIHRhcmdldCA9PSAwOiByZXR1cm4gMQoJZWxpZiBmbGlwcyA9PSAwOiByZXR1cm4gMAoJZWxzZTogcmV0dXJuICgKCQlydXNzZWxzL3JlbWFpbmluZyAgICAgICAgICAgICAqIGNoYW5jZShmbGlwcy0xLCBydXNzZWxzLTEsIHRhcmdldC0xLCByZW1haW5pbmctMSkgKwoJCShyZW1haW5pbmctcnVzc2VscykvcmVtYWluaW5nICogY2hhbmNlKGZsaXBzLTEsIHJ1c3NlbHMsIHRhcmdldCwgcmVtYWluaW5nLTEpCgkpCgkKZGVmIHBwKHgpOgoJaWYgeCA8IDAuMTogcmV0dXJuICJ7Oi4yJX0iLmZvcm1hdCh4KQoJZWxzZTogcmV0dXJuICJ7Oi4xJX0iLmZvcm1hdCh4KQoKcHJpbnQoImZsaXBzL3J1c3NlbHN8ICAgMSAgIHwgICAyICAgfCAgIDMgICB8ICAgNCAgIHwgICA1ICAgfCAgIDYgICB8ICAgNyAgIHwgICA4IikKcHJpbnQoIi0tLS0tLS0tLS0tLS0rLS0tLS0tLSstLS0tL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