All problems
Gap Buffer Insert
Problem
Simulate a gap buffer text editor.
text is the initial text and operations contains ["move", pos] (move the cursor to an absolute position, clamped to both ends), ["insert", s] (insert a string at the cursor and leave the cursor after it), or ["text"] (record the current string).
Return the list of recorded texts (None for move and insert).
Hint
The gap sits between the left and right buffers; moving the cursor slides characters across it.
Test cases
case 1
in: ["abc",[["move",1],["insert","XY"],["text"],["move",0],["insert","z"],["text"]]]
expected: [null,null,"aXYbc",null,null,"zaXYbc"]
case 2
in: ["",[["insert","hi"],["text"]]]
expected: [null,"hi"]
case 3
in: ["ab",[["move",5],["insert","X"],["text"]]]
expected: [null,null,"abX"]
case 4
in: ["ab",[["text"]]]
expected: ["ab"]
Starter codePython
def gap_buffer_ops(text, operations):
# Your code here
pass4 test cases · run your code against them in the editor