Write a program that converts a bit string into robot commands (decoding). The code reflects command frequencies (shorter for common commands).
Bit Encoding -> Command
0 -> Step
10 -> TurnLeft
110 -> TurnRight
1110 -> Pick
1111 -> Drop
Example 1 (click to see the figure):
Enter a sequence of bits: 11101101100100
Pick TurnRight TurnRight Step TurnLeft Step
In Example 1,
program input: 11101101100100
program output: Pick TurnRight TurnRight Step TurnLeft Step
More input-output samples.
The input sequence of bits is decoded from left to right.
You can assume that all the input sequences are leagal.
The last output character can be a space. That is, the following program output for Example 1 is also legal:
Pick TurnRight TurnRight Step TurnLeft Step
It is recommended that your solution is written based on the code template.
Do not output any line breaks except the one in the code template.
Do not modify the cout statements in the code template.
Do not use getline(cin, code); otherwise your assignment will not be processed.
To check whether an input bit is 0, use code[i] == '0' or code.at(i) == '0'
Treat code as a character array, where each element represetns a single character.
Remember that the character '0' is distinct from the integer 0.
Using code.at(i) is preferable to code[i], because if index i is out of range, code.at(i) will throw an error, while code[i] will not detect this issue.
Make sure that the array index is within its valid range.
When using code[i], ensure that i >= 0 and i <= code.length()-1
When using code[i+1], ensure that i+1 >= 0 and i+1 <= code.length()-1
Accessing an index outside of this range can lead to runtime errors or unpredictable behavior.
If a runtime error occurs, the program's accuracy will be considered ZERO.
If you are interested in the story behind this assignment, read Demystify Entropy for the motivation of encoding.