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 brownie57
def checkio(marbles: str, step: int) -> float:
import itertools
ans = 0
wbproduct = list(itertools.product('wb', repeat=step))
for i in wbproduct:
if i[-1] == 'w':
p = 1
n = len(marbles)
nw = marbles.count('w')
for s in i:
if s == 'w':
p *= nw / n
nw -= 1
else:
p *= (1 - nw / n)
nw += 1
ans += p
return round(ans, 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. 14, 2018
Comments: