You are viewing a single comment's thread. Return to all comments →
This is my class based python solution
APPEND = 1 DELETE = 2 PRINT = 3 UNDO = 4 class TextEditor: def __init__(self, initial_str: str = ""): self.current_str: list = list(initial_str) # ["h", "e", "l", "l", "o"] self.stack = [] # stack will save for undo ["hello", "helloworld"] def append(self, s: str): self.stack.append("".join(self.current_str)) self.current_str.extend(list(s)) def delete(self, k: int): self.stack.append("".join(self.current_str)) for _ in range(k): _ = self.current_str.pop() def print(self, k): print(self.current_str[k-1]) def undo(self): self.current_str = list(self.stack.pop()) def command(self, s: str): op = int(s[0]) if op == APPEND: self.append(s.split(" ")[-1]) elif op == DELETE: self.delete(int(s.split(" ")[-1])) elif op == PRINT: self.print(int(s.split(" ")[-1])) elif op == UNDO: self.undo() if __name__ == "__main__": Q = int(input()) editor = TextEditor() for _ in range(Q): command = input() editor.command(command)
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
This is my class based python solution