You are viewing a single comment's thread. Return to all comments →
python 3 (OOP)
class text_editor: def __init__(self): self.S = "" self.op_stack = [] self.tgt_stack = [] def w_append(self,tgt): s1 = list(self.S) s1.append(tgt) self.S = ''.join(s1) self.op_stack.append('1') self.tgt_stack.append(len(tgt)) def k_delete(self,tgt): s1 = list(self.S) self.tgt_stack.append(''.join(s1[-int(tgt):])) s1 = s1[:-int(tgt)] self.S = ''.join(s1) self.op_stack.append('2') def k_print(self,tgt): s1 = list(self.S) print(f'{s1[int(tgt) -1]}') def undo(self): s1 = list(self.S) op = self.op_stack.pop(-1) if op == '1': s1 = s1[:-int(self.tgt_stack.pop(-1))] elif op == '2': s1.append(self.tgt_stack.pop(-1)) if s1: self.S = ''.join(s1) else: self.S = "" def receive_ops(self,op,tgt): if op == '1': self.w_append(tgt) elif op == '2': self.k_delete(tgt) elif op == '3': self.k_print(tgt) elif op == '4': self.undo() if __name__ == '__main__': q = int(input()) S = "" tgt = None op = '' t = text_editor() for _ in range(q): x = input().split() op = x[0] if len(x) > 1: tgt = x[1] t.receive_ops(op,tgt)
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 →
python 3 (OOP)