simple and c solution in Clear category for Pearls in the box by
def checkio(marbles, step):
length = len(marbles)
def cal_chance(new_marbles, next_step):
# chance of draw white pearls
w_chance = new_marbles.count('w') / length
# new marbles after draw a white pearl
w_change = new_marbles.replace('w', 'b', 1)
# chance of draw black pearls
b_chance = 1 - w_chance
# new marbles after draw a black pearl
b_change = new_marbles.replace('b', 'w', 1)
# if left step is greater than 1,
# we will do the above step again on both the two conditions ,
# and add up their result.
# Every time we go a step further, the chance should be multiplied
next_step -= 1
while next_step > 0:
return w_chance * cal_chance(w_change, next_step) + \
b_chance * cal_chance(b_change, next_step)
result = float('%0.2f' % cal_chance(marbles, step))
# 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"
June 1, 2016