Your question is Stack-Based Text Editor Undo/Redo. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Implement the editing history for a simple text editor used within a Lutron Designer configuration workflow. The editor supports inserting text, deleting characters, undoing edits, and redoing previously undone edits.
Use two stacks: an undo stack and a redo stack. Every successful insert or delete saves the text state before the edit on the undo stack and clears the redo stack. An undo restores the most recent previous state and places the current state on the redo stack. A redo restores the most recently undone state and places the current state on the undo stack. If an undo or redo is requested when its stack is empty, do nothing.
Implement process_editor(initial_text, operations).
initial_text is a string.operations is a list of dictionaries.{"type": "insert", "index": i, "text": s}.{"type": "delete", "index": i, "count": c}.{"type": "undo"} or {"type": "redo"}.Example 1: initial_text = "Lutron", operations = [{"type":"insert","index":6,"text":" Hub"},{"type":"undo"}] returns "Lutron" because the insertion is undone.
Example 2: initial_text = "abc", operations = [{"type":"delete","index":1,"count":1},{"type":"undo"},{"type":"redo"}] returns "ac" because the deletion is undone and then reapplied.
def process_editor(initial_text, operations):