FunctionRacerProblemsRace

Add the Fractions

MediumStringsPython, JavaScript, TypeScript, Java, C++

expression adds and subtracts fractions, like -1/2+1/3-1/6. Every fraction is written top/bottom with a top from 0 to 10 and a bottom from 1 to 10. Between two fractions stands a + or a -, and the first fraction may have a - in front. There are no spaces.

Return the result as one fraction in lowest terms, written top/bottom with a positive bottom. A whole result keeps the bottom 1: two is 2/1 and zero is 0/1.

Example 1

expression "1/2-1/3"
returns "1/6"

One half minus one third is one sixth.

Example 2

expression "-3/4+3/4"
returns "0/1"

Minus three quarters plus three quarters is nothing.

Example 3

expression "2/5+8/5"
returns "2/1"

Two fifths and eight fifths make ten fifths, which is 2.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems