PROBLEM STATEMENT
Interpret a short script and return the value of the given variable enclosed in
a string once the script is done.
If the script will execute infinitely, return "".
Variable names are one character in length, and do not need to be declared.
They should be initialized to zero.
The four statements in this scripting language are LET, GOTO, END, and
IF-THEN-GOTO.
LET is used to assign a value to a variable.
GOTO is used to jump to another line in the script.
END is used to halt execution immediately.
IF-THEN-GOTO is used to jump to another line in the script if an expression is
true.
Spaces are used to separate keywords and values, etc. Any number of spaces may
be found between keywords or at the beginning or end of a
statement.
DEFINITION
Class Name: Script
Method Name: evaluate
Parameters: String[], String
Returns: String
Method signature: String evaluate(String[] sourceCode, String variableToWatch)
Be sure your method is public.
NOTES
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
*sourceCode has between 1 and 50 elements, inclusive.
*each element of sourceCode contains between 3 and 50 characters, inclusive.
*all elements of source code consist of only spaces, upper-case letters, the
digits '0' - '9', and the characters '<', '>', '=', and '!'.
*variableToWatch is between 'A' and 'Z', inclusive.
*Each element of sourceCode is a line of code, with keywords, variable names,
integer values, and symbols separated by spaces (the exact number of spaces is
not specified).
*Whenever a line number is given in the script, that line number must be a
valid index into sourceCode (You may not be redirected to a line of code that
doesn't exist).
*The first non-space sub string of an element of sourceCode is a command
keyword. Command keywords are:
GOTO
LET
END
IF
NOTE: TopCoder will ensure that each line in sourceCode follows the correct
format:
These formats are:
GOTO is followed by an integer value.
Format: "GOTO <line number>"
<line number> is a valid index into sourceCode.
If this type of statement is reached, the program should move to the element of
sourceCode represented by <line number>.
LET is followed by a variable name, an equals sign '=', and an integer value.
Format: "LET <var name> = <new value>".
<var name> is a character between 'A' and 'Z', inclusive.
<new value> is an integer value between -1000 and 1000, inclusive.
If this type of statement is found, put the value <new value> into the variable
represented by <var name>.
END stands alone. Program execution should halt if this statement is ever
reached.
Format: "END"
IF is followed by a variable name, ('A'-'Z'), a comparison sign
('=','<','>','!=','<=','>='), an integer value, the keyword THEN, the
keyword GOTO, and an integer value.
Format: "IF <var name> <op> <compare to value> THEN GOTO <line number>"
<var name> is a character between 'A' and 'Z', inclusive.
<op> is one of '=','<','>','!=','<=','>='. The first op is treated the same as
== in Java or C++.
The rest are all treated as they are treated in Java or C++.
<compare to value> is an integer between -1000 and 1000, inclusive.
It will be compared to the value in <var name> with the comparison code <op>.
<line number> is an integer value that is a valid index into sourceCode. That
is, if <line number> is 5, it refers to element 5 of sourceCode.
If the expression represented by <var name> <op> <compare to value> is true,
then the script should execute the "GOTO <line number>" part of the statement.
*Your method should execute this "script" one line at a time.
*If the current statement does not move the point of execution to some specific
line number, continue executing the script with the next element of sourceCode.
*Variables should be logged as assignments are made (there is no variable
declaration).
*If the variable asked for is not ever assigned a value, it should be assigned
the value 0.
*The index of an element of sourceCode is the line number.
*If the last line of the script ever executes and the statement at that line
does not direct the program to go to some other line, the script ends.
Examples:
sourceCode = {
"GOTO 5 ",
"LET A = 5",
" LET B = 10",
"LET C = 10",
"GOTO 6",
"GOTO 1",
"END" },
variableToWatch = 'B'
Code Execution is as follows:
0. GOTO 5
5. GOTO 1
1. LET A = 5
2. LET B = 10
3. LET C = 10
4. GOTO 6
6. END
B has the value 10 in it. Return "10".
sourceCode = {
" GOTO 0",
"LET A = 5",
" END " },
variableToWatch = 'Z'
This generates an infinite loop. Return "".
sourceCode = {
"LET A = 5",
"IF B >= 6 THEN GOTO 4 ",
"LET B = 6",
"GOTO 1",
"END" },
variableToWatch = 'B'
The script executes in this order:
0. "LET A = 5"
1. "IF B >= 6 THEN GOTO 4 "
2. "LET B = 6"
3. "GOTO 1"
1. "IF B >= 6 THEN GOTO 4 "
4. "END"
B holds the value 6 so return "6".
Test cases:
{ "GOTO 0" }, "A" returns ""
{ "LET A = 5", "IF A >= 1000 THEN GOTO 3", "LET B = 2", "END" }, "B" returns "2"
{ "LET A = 1", "LET B = 6", "LET C = 8", "IF C < 1 THEN GOTO 0", "LET D = 7",
"GOTO 9", "LET A = 4", "LET A = 5", "GOTO 10", "GOTO 6",
"END" }, "A" returns "5"