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 jiachxxj
def checkio(marbles: str, step: int) -> float:
def checkio_in(marbles, step):
if marbles == '':
return 0
w = marbles.count('w')
b = marbles.count('b')
if step == 1:
return w/(w+b)
new_marbles1 = 'w'*(w-1)+'b'*(b+1) if w>=1 else ''
new_marbles2 = 'w'*(w+1)+'b'*(b-1) if b>=1 else ''
return (w/(w+b))*checkio_in(new_marbles1,step-1) + (b/(w+b))*checkio_in(new_marbles2,step-1)
return round(checkio_in(marbles, step),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!")
April 26, 2019