Skip to content

Instantly share code, notes, and snippets.

@sang-d
Last active August 26, 2017 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sang-d/960f95ede0b634a16393d8d06047bb54 to your computer and use it in GitHub Desktop.
Save sang-d/960f95ede0b634a16393d8d06047bb54 to your computer and use it in GitHub Desktop.
Q = int(input().strip(' '))
# print(Q)
class Node():
def __init__(self, val=None, next=None):
self.val = val
self.next = next
class Stack():
def __init__(self, head=None):
self.head = head
def pop(self):
if self.head:
node = self.head
self.head = node.next
return node.val
else:
return None
def push(self, val):
if self.head:
self.head = Node(val, self.head)
else:
self.head = Node(val, None)
class Editor():
def __init__(self):
self.states = Stack()
self.text = ''
def execute(self, operation):
l = len(self.text)
parts = operation.split(' ')
t = int(parts[0])
if t == 1:
self.states.push(self.text)
self.text += str(parts[1])
if t == 2:
self.states.push(self.text)
k = int(parts[1])
if k >= l:
self.text = ''
else:
self.text = self.text[:l-k]
if t == 3:
k = int(parts[1])
print(self.text[k-1])
if t == 4:
self.text = self.states.pop()
editor = Editor()
for i in range(Q):
operation = input().strip()
editor.execute(operation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment