Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Pearls in the Box by Rounin
def checkio(marbles, step):
outer = False
if str(type(marbles)) == "":
outer = True
marbles = sorted(list(marbles))
else:
marbles = marbles.copy()
blackMarbles = (marbles.index('w') if 'w' in marbles else len(marbles))
whiteMarbles = len(marbles) - blackMarbles
blackProbability = blackMarbles / len(marbles)
whiteProbability = 1.0-blackProbability
if step > 1:
newP = 0
if blackMarbles > 0:
marbles[blackMarbles-1] = 'w'
newP += blackProbability*checkio(marbles, step-1)
if blackMarbles > 0:
marbles[blackMarbles-1] = 'b'
if blackMarbles < len(marbles):
marbles[blackMarbles] = 'b'
newP += whiteProbability*checkio(marbles, step-1)
whiteProbability = newP
return ((round(whiteProbability * 100.0))/100.0 if outer else whiteProbability)
#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"
July 29, 2017