def share_money money parts money_parts for in xrange len parts if len

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def share_money(money, parts):
money_parts = []
for x in xrange(len(parts)):
if x + 1 == len(parts):
money_parts.append(money)
else:
sum = reduce(lambda x, y: x+ y, parts[x:])
part = parts[x]
money_part = round((money / sum) * part, 2)
money -= money_part
money_parts.append(money_part)
print money_parts
return money_parts
assert [1, 1] == share_money(2, [0.5, 0.5])
assert [1, 1, 1] == share_money(3, [0.33, 0.33, 0.33])
assert [0.75, 2.25] == share_money(3, [0.25, 0.75])
assert [0.5, 0.3, 0.2] == share_money(1, [0.5, 0.3, 0.2])
assert [1] == share_money(1, [1])