Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
fractions.Fraction solution in Clear category for Fractions Addition by kurosawa4434
from fractions import Fraction
def add_fractions(fracts):
total = sum(Fraction(*f) for f in fracts)
whole = total.numerator // total.denominator
rest = total - whole
if whole and rest:
return f'{whole} and {rest}'
elif whole:
return whole
else:
return f'{rest}'
if __name__ == '__main__':
print("Example:")
print(add_fractions(((2, 3), (2, 3))))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert add_fractions(((2, 3), (2, 3))) == "1 and 1/3"
assert add_fractions(((1, 3), (1, 3))) == "2/3"
assert add_fractions(((1, 3), (1, 3), (1, 3))) == 1
print("Coding complete? Click 'Check' to earn cool rewards!")
April 24, 2018
Comments: