Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First Fraction solution in Clear category for Fractions Addition by dig
from fractions import Fraction
def add_fractions(fracts):
suma=0
for num, den in fracts:
suma += Fraction(num,den)
integer_part = int(suma)
decimal_part = suma - integer_part
if integer_part == suma:
return integer_part
elif not integer_part:
return str(suma)
else:
return str(integer_part)+ ' and ' + str(decimal_part)
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 26, 2023