FunctionRacerProblemsRace

Cursor in a Line of Text

EasyClassesPython, JavaScript, TypeScript, Java, C++

Implement a class LineEditor that edits one line of text through a cursor. The cursor sits between characters: position 0 is before the first character and position len(line) is after the last one. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

LineEditor()
insert_text("helo") returns 4
move_cursor(-1) returns 3
insert_text("l") returns 4
move_cursor(10) returns 5
delete_chars(-2) returns "lo"
get_line() returns "hel"
move_cursor(-9) returns 0
delete_chars(2) returns "he"
get_line() returns "l"

The l goes in before the o, making hello with the cursor at 4. Moving 10 right stops at the end, 5. Backspacing two deletes lo, and after jumping to the start, deleting two to the right removes he.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems