← Back to challenges

Fractions Addition and Subtraction

PythonHardalgorithmsnumbersregexstrings

Instructions

The function is given a string representing an expression of many fractions addition and subtraction. Compute the arithmetic result and return the string in the form of an irreducible fraction. Even if the result is an integer, e.g. 3, return it as 3/1.

Examples

fractions_addition("-1/2+1/2") ➞ "0/1"

fractions_addition("-1/2+1/2+1/3") ➞ "1/3"

fractions_addition("1/3-1/2") ➞ "-1/6"

fractions_addition("5/3+1/3") ➞ "2/1"

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.