Problem 1003. Make a KITT-scanner on the command line
Did you know that you can actually remove characters from the command-line window? Just send a 'backspace' character to the output, e.g. with fprintf(char(8)); (ASCII code 8 is a backspace), and the last character is removed. This way, you can write a line, and remove it afterwards, by sending as many backspace characters as the line was long. However, you only move the cursor back, so when you want to clear a line, you have to move first the cursor to its beginning, then overwrite the line with blanks (spaces, ' '), and then move the cursor back once more. E.g. with
fprintf(repmat([char(8) ' ' char(8)],lenght_of_last_line,1));
Your task is to program a KITT-scanner (the red sweeping light in front of the black car in the TV-series Knight Rider) on the command line. The bar has a width specified by the first parameter L of the function. The characters which the bar is made of are specified in the 2nd parameter, S, of the function. The first character of S is the actual light. The last character is 'background', and the tail of the light is defined by the characters in between. For example
kitt(10,'#=~-')
tells it to show up as
'#=~-------'
It starts with the light on the left, tail to the right, and the light moving to the right in the next step. So,
'=#--------'
followed by
'~=#-------'
and
'-~=#------'
and so on. You see, the headlight supersedes the tail, when the head and tail overlap. Otherwise, the characters just show up with in the order defined in S. The 'frame rate' of the scanner should be 1/2 Hz, or one sweep back and forth in 2 seconds, but this can not be checked by Cody. But you are encouraged to watch the result on your own screen. To check your code, the function should output a character array with the full sequence, until the first step is repeated, with every row a step in the sequence (including the repeated last step). And off course, try to avoid just hard-coding the result.
Solution Stats
Problem Comments
-
2 Comments
The '-' in line 6 of y_correct for test case 4 should be a '~'.
You are absolutely correct. My apologies for this mistake. I should have tested this, and I thought I had for most testcases.
Solution Comments
Show commentsProblem Recent Solvers6
Suggested Problems
-
10099 Solvers
-
17070 Solvers
-
21185 Solvers
-
218 Solvers
-
682 Solvers
More from this Author31
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!