We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
if name == 'main':
text = []
edition = []
for _ in range(int(input())):
op = input().split()
if op[0] == '1':
text.extend(op[1])
edition.append(len(op[1]))
elif op[0] == '2':
edition.append([])
for _ in range(int(op[1])):
edition[-1].append(text.pop())
elif op[0] == '3':
print(text[int(op[1])-1])
elif op[0] == '4':
undo = edition.pop()
if isinstance(undo, int):
for _ in range(undo):
text.pop()
else:
text.extend(undo[::-1])
Cookie support is required to access HackerRank
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 →
My python solution
if name == 'main': text = [] edition = [] for _ in range(int(input())): op = input().split() if op[0] == '1': text.extend(op[1]) edition.append(len(op[1])) elif op[0] == '2': edition.append([]) for _ in range(int(op[1])): edition[-1].append(text.pop()) elif op[0] == '3': print(text[int(op[1])-1]) elif op[0] == '4': undo = edition.pop() if isinstance(undo, int): for _ in range(undo): text.pop() else: text.extend(undo[::-1])