The challenge is to manipulate strings using basic Vim-like commands
Input:
- A string of text to be transformed. The input alphabet is [a-z].
- A string of legal commands
Output:
- The transformed string
There are a few ideas you should understand:
The cursor is represented as a location between 2 characters. |
will be used to represent the cursor(s) in this explanation.
|foobar
- The cursor at beginning of stringfoo|bar
- Cursor in the middle of the stringfoobar|
- Cursor at the end of the string
A selection is a a group of characters between 2 cursors.
|foobar|
- The selection isfoobar
|foo|bar
- The selection isfoo
foo|bar|
- the selection isbar
|foobar
- The selection is the empy string (both cursors at same location)
A selection is formed from the previous cursor position and the result of the current command.
Using the command >
on |foo
would make the selection |f|oo
.
Commands can be described as functions. C<selection>
is a command that has a selection parameter. The next command provides the selection.
D>
deletes the character to the right of the cursor.f|oo
->
f|o
DNa
deletes from the cursor to the nexta
characterf|oobar
->
f|ar
- Etc.
As you can see, commands "associate to the right"
DNa
is parsed as(D(Na))
DDNa
is parsed as(D(D(Na)))
- Etc.
The insert mode command I
extends until the next command.
DIbar>
is parsed as(D(Ibar))(>)
.
These are the commands:
>
- Move cursor right.>
onfoo|bar
->
foob|ar
<
- Move cursor left.<
onfoo|bar
->
fo|obar
B
- Move cursor to the beginning of the stringE
- Move cursor to the end of the stringN<char>
- Move cursor to place before next<char>
.Na
on|foobar
->
foob|ar
P<char>
- Move cursor to place after previous<char>
.Po
onfoobar|
->
foo|bar
I<string>
- Enter/exit insert mode. Characters in<string>
are inserted at the current cursor position.<string>
extends until the next command. The cursor is moved to end of insertion<string>
.Iquz
onfoo|bar
->
fooquz|bar
D<selection>
- delete characters in<selection>
.f|ooba|r
->
f|r
. The output selection from this command is the empty string.
Notes:
- The cursor cannot move out of bounds.
B<<<
is the same asB
.Nx
is the same asE
if nox
can be found. - The cursor location always starts at the beginning of the string
- The cursor should not be outputted
- Commands are always uppercase, characters are lowercase.
Restrictions:
- No text editor scripts like Vimscript
Winning criteria:
- It's code golf. Lowest character count wins.
Examples:
Input:
foobar
>>><<>If
Output:
fofobar
Input:
foobar
>>>DNEDPf
Output:
f
Input:
foobar
>>>DDDDIbazD<Iquz>DPfDII
Output:
far
<string>
extends until the next command." But how do we know that it's a command rather than part of the string? \$\endgroup\$