You are viewing a single comment's thread. Return to all comments →
class Stack: def __init__(self): self.stacks = "" self.undo = [] def append(self, val): self.undo.append(self.stacks) self.stacks += val def delete(self, index): val = self.stacks[:len(self.stacks)-index] self.undo.append(self.stacks) self.stacks = val def print_val(self, index): print(self.stacks[index-1]) def undo_operation(self): if self.undo: last = self.undo.pop() self.stacks = last Q = int(input()) stack = Stack() for _ in range(Q): operation = input().split() t = int(operation[0]) if t == 1: stack.append(operation[1]) elif t == 2: stack.delete(int(operation[1])) elif t == 3: stack.print_val(int(operation[1])) elif t == 4: stack.undo_operation()
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 →