Dead code sits there doing nothing, staring at us knowing it will never be executed... but today we can take revenge.
Specification
The input will be a multiline string.
Each line may either be an assignment or an expression .
Assignment
An assignment is of the form <name> = number
where name is a sequence of letters, underscores and numbers, but not starting with a number.
Variables may be assigned any number of times.
Expression
An expression is of the form <var_name OR number> <operation> <var_name OR number> ...
An expression may be any combination of:
- Variables already defined
- Basic arithmetic operators
+-*/
- Numbers (integers)
Expected output
You should output the string with redundant assignments, assignments that are never used by any of the expressions following it, removed. Please note that assignments can also be made redundant if an additional assignment to the same variable is performed before an expression using the variable is executed.
Test cases
in
a = 10
a * 3
out
a = 10
a * 3
in
foo = 8
2 - 1
a = 18
out
2 - 1
in
a = 10
a = 8
b = 4
ab = 72
b / 6
b + 1
out
b = 4
b / 6
b + 1
in
a = 1
a = 2
a + 1
out
a = 2
a + 1
in
FooBar1 = 0
Fuz__ = 8
Fuz__ / 1
out
Fuz__ = 8
Fuz__ / 1
in
a = 1
a + 1
a = 2
a + 1
out
a = 1
a + 1
a = 2
a + 1
in
a = 1
1 / 5 * 8 + 4
out
1 / 5 * 8 + 4
in
a = 1
a + 1
a = 1
a + 1
out
a = 1
a + 1
a = 1
a + 1
in
a = 7
5 / a
out
a = 7
5 / a
a = 1; a + 1; a = 1; a + 1;
? Where the seconda = 1
can be discarded only becausea
was previously set to the same value (1
). \$\endgroup\$