FunctionRacerProblemsRace

Solve for x

MediumStringsPython, JavaScript, TypeScript, Java, C++

equation is an equation in one unknown, such as 3x-4+x=2x+6. It has exactly one =. Each side is one or more terms joined by + and -, and the first term of a side may have a - in front. A term is a whole number (12), a whole number followed by x (3x, also 0x), or just x. There are no spaces, brackets or other signs.

Return "x=N" when exactly one number N solves the equation (it is always a whole number then), "all" when every number solves it, and "none" when no number does.

Example 1

equation "3x-4+x=2x+6"
returns "x=5"

The left side is 4x - 4 and the right side is 2x + 6, so 2x is 10 and x is 5.

Example 2

equation "x+1=1+x"
returns "all"

Both sides are the same, whatever x is.

Example 3

equation "x=x+1"
returns "none"

The x cancels out and leaves 0 = 1.

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