Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
7-liner: simple solution in Clear category for Pearls in the Box by przemyslaw.daniel
def checkio(pearls, step):
black = pearls.count('b')/len(pearls)
white = pearls.count('w')/len(pearls)
if step == 1: return white
black = black*checkio(pearls.replace('b', 'w', 1), step-1)
white = white*checkio(pearls.replace('w', 'b', 1), step-1)
return black+white
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"
May 3, 2016