A higher resolution is required to access the IDE
- 9
Learning Opportunities
This puzzle can be solved using the following concepts. Practice using these concepts and improve your skills.
Statement
Goal
This puzzle builds on the concepts explored in the puzzle 24: The Long Game by @jddingemanse. Significant insight can be gained by solving that puzzle before working through this one:https://www.codingame.com/training/hard/24-the-long-game
In this puzzle, you are given an equation with a variable x somewhere in that equation. Your task is to REARRANGE the terms of the expressions on either side of the equal sign to produce an equation in the following form:
The expression on the right side of the equal sign must be in "standard form" per the rules identified in 24: The Long Game. For your convenience, those rules are copied below. To see these rules applied to a sample expression, please refer back to 24: The Long Game.
EXPRESSIONS, TERMS AND SUBEXPRESSIONS
To understand the rules for rewriting expressions, we must distinguish expressions, terms and subexpressions.
expression: Terms separated by mathematical operators that can be evaluated from left to right. For example:
term: A constant or a subexpression. The expression
subexpression: A subexpression is an expression that must be evaluated before the parent expression can be evaluated. The expression
REWRITING EXPRESSIONS
Only rewrite an expression such that it remains mathematically the same.
Rewrite expressions according to the following three rules:
1. Use as few parentheses as possible.
- Remove unnecessary parentheses:
- Rewrite an expression, if necessary:
2. Sort operators:
- Change the order of terms such that for their connecting operators
- Do this within a parent expression:
- Do this within a subexpression:
- This does not apply to operators in two different subexpressions: in
3. For cases not decided by rule 2, sort terms of expressions and subexpressions
a) Sort constants in ascending order:
b) Place constants before subexpressions:
c) Sort two subexpressions according to their numerical value:
In the case of equal value, sort the subexpressions as strings, based on the ASCII value of characters:
Input
Line 1: An integer numEquations indicating the number of equations to be solved (rearranged).
Next numEquations lines: A string equation.
Next numEquations lines: A string equation.
Output
Line 1: A string 'x= expression' with the expression rewritten in standard form.
Constraints
numEquations <= 40
length(equation) < 100
equation is always a valid equation and contains only onex , one = , and the characters 0 to 9 , + , - , * , / , ( and ) .
The following two constraints ensure each equation is in the set of equations fully covered by the rules of 24: The Long Game.
There is no explicit zero term anywhere in any equation.
No expression or subexpression in the equation will ever begin with a negative sign (- ).
length(equation) < 100
equation is always a valid equation and contains only one
The following two constraints ensure each equation is in the set of equations fully covered by the rules of 24: The Long Game.
There is no explicit zero term anywhere in any equation.
No expression or subexpression in the equation will ever begin with a negative sign (
Example
Input
6 3+x=7 x+44=6 18=29+x 310=x+429 x+4+9+2+23+18=13+26+3+1 8+4+10+67+234=x+87+98+12+65
Output
x=7-3 x=6-44 x=18-29 x=310-429 x=1+3+13+26-2-4-9-18-23 x=4+8+10+67+234-12-65-87-98
A higher resolution is required to access the IDE