33
1
Your job is to simulate a couple of keystrokes that a user types in.
Input
A string array or string with a delimiter of your choice (outside the range 32-126) containing at least one 'keystroke'.
This array will only contain two types of string: passive keystrokes (single chars) and commands (chars within brackets [ ]
).
- Passive keystrokes
- ASCII character codes
[32-126]
- ASCII character codes
- Commands:
[B]
: backspace (remove last character added if there is one)[C]
: copy all of what has already been written[D]
: delete all of what has been written[P]
: paste what has been copied
Output
The string produced by the keystrokes.
Examples
['H', 'e', 'l', 'l', 'o'] -> 'Hello'
['H', 'e', 'l', 'l', 'o', ' ', '[C]', '[P]'] -> 'Hello Hello '
['[D]', 'D', '[B]'] -> ''
['H', '[C]', 'i', '[P]', '[C]', '[P]'] -> 'HiHHiH'
['e', '[C]', '[B]', 'I', ' ', 'l', 'i', 'k', '[P]', ' ', 'b', '[P]', '[P]', 's', '!'] -> I like bees!
['N', '[P]'] -> 'N'
['#', '5', '0', 'K', '0', '0', '1', '[D]', '#', 'n', 'o', 't'] -> '#not'
['H', 'o', 'w', ' ', '[D]', 'H', 'e', 'y'] -> 'Hey'
['s', 'u', 'd', '[B]', 'p', '[C]', '[D]', 'I', ' ' , 'h', 'a', 'v', 'e', ' ', '[P]', 'p', 'e', 'r', '!'] -> 'I have supper!'
This is code-golf, so shortest code in bytes wins!
FYI I found a bug in my code, may I suggest a test case with a capital B for bees :) – Jonathan Allan – 2016-10-10T01:19:16.413
There should also be an example with
[D]
where it's not the only one used, so the code won't just besplit('[D]')[1]
or something. – mbomb007 – 2016-10-10T13:25:27.267@mbomb007, I added test cases (#8, #9) – Daniel – 2016-10-10T13:32:12.557
And I think mbomb007 was asking for a test case with multiple deletions. – Martin Ender – 2016-10-10T13:35:34.643
@MartinEnder, oh I thought he wanted one where there are commands other than
[D]
– Daniel – 2016-10-10T13:36:36.550