Problem Statement |
| In this problem, you will simulate a program that controls the actions of a progress indicator. The indicator is a single bar character in the middle of the screen with one of 4 states: '|', '-', '\', and '/'. The program is given as a sequence of instructions in the form:
<instr> <secs>
where <instr> represents one of 4 possible actions, and <secs> is the action's duration in seconds. The action is performed once each second. The 4 possible actions are::
- 'L': Spin left. If the bar is in state '|', it goes to '\'. State '\' goes to '-', '-' goes to '/', and '/' goes to '|'.
- 'R': Spin right. This is the exact opposite of 'L': '\' goes to '|', '|' goes to '/', '/' goes to '-', and '-' goes to '\'.
- 'S': Stay. The bar remains in its current state.
- 'F': Flip. The bar is rotated 90 degrees: '\' becomes '/', '/' becomes '\', '-' becomes '|', and '|' becomes '-'.
So, the sequence "F03L02" and the starting state of '-' leads to the following sequence: "-|-|\-".
You are given a program and a startState. Return a String containing the sequence of states produced by the program. The ith character of the String is the state of the progress indicator after i seconds. At time 0, the indicator is in its initial state, so the first character of the return value is always startState. |
|
Definition |
| Class: | IndicatorMotion | Method: | getMotion | Parameters: | String, char | Returns: | String | Method signature: | String getMotion(String program, char startState) | (be sure your method is public) |
|
|
|
|
Notes |
- | In the examples the character '\' appears as '\\' because of the C++/Java syntax for escaping characters. |
|
Constraints |
- | startState will be '|', '-', '\' or '/'. |
- | program will contain exactly 3*k characters, where k is an integer between 0 and 10, inclusive. |
- | For each k, the (3*k)-th character in program will be one of 'L', 'R', 'F' or 'S'. |
- | For each k, the (3*k+1)-th and (3*k+2)-th characters of program will be digits ('0'-'9'). |
|
Examples |
0) | |
| | Returns: "-|-|\\-\\|///\\/\\/" | This leads to the following sequence of states (below each state is the action performed during that second):
-|-|\-\|///\/\/
.FFFLLRRRSSFFFF
|
|
|
1) | |
| | Returns: "/\\/\\/\\/\\/\\/-|-|-|-|-|-" | |
|
2) | |
| | Returns: "/" | Watch out for empty programs! |
|
|
3) | |
| |