20
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
1Should you add this particularly difficult case:
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
). – flodel – 2015-08-29T22:01:32.2503@flodel No, no need to look at values – Caridorc – 2015-08-29T22:02:32.350
@flodel testcase incorporated – Caridorc – 2015-08-29T22:06:12.153
You should add a test case where a variable is used in an expression, but not as the first element of the expression. Especially important is as the last member of the expression. – isaacg – 2015-08-30T01:55:11.003
@isaacg no dead code, the var may be anywhere, testcase added – Caridorc – 2015-08-30T08:06:49.700