Last active
August 26, 2017 11:16
-
-
Save sang-d/960f95ede0b634a16393d8d06047bb54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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