Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Short with fractions.Fraction solution in Clear category for Oil Pie by lskukan
from fractions import Fraction
def divide_pie(groups):
# Number of drones supposed to eat pie
droneNum = sum(map(abs, groups))
# Remaining percentage of the pie is a fraction
remaining = Fraction(1)
for group in groups:
# If drones know how big the pie is, they eat their share
# (their_number/full_number)
if group > 0:
remaining -= Fraction(group, droneNum)
# Otherwise, they eat their share of what they suppose the pie is
# (their_number/full_number) * remaining_share
elif group < 0:
remaining -= remaining * Fraction(abs(group), droneNum)
return remaining.numerator, remaining.denominator
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance((2, -2), (tuple, list)), "Return tuple or list"
assert tuple(divide_pie((2, -1, 3))) == (1, 18), "Example"
assert tuple(divide_pie((1, 2, 3))) == (0, 1), "All know about the pie"
assert tuple(divide_pie((-1, -1, -1))) == (8, 27), "One by one"
assert tuple(divide_pie((10,))) == (0, 1), "All together"
July 29, 2014
Comments: