-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleTextEditor.py
48 lines (41 loc) · 1.28 KB
/
SimpleTextEditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Enter your code here. Read input from STDIN. Print output to STDOUTimport os
import os
class Stack:
def __init__(self):
self.items = []
#method to check the stack is empty or not
def isEmpty(self):
return self.items == []
#method for pushing an item
def push(self, item):
self.items.append(item)
#method for popping an item
def pop(self):
return self.items.pop()
#check what item is on top of the stack without removing it
def peek(self):
return self.items[-1]
#method to get the size
def size(self):
return len(self.items)
#to view the entire stack
def fullStack(self):
return self.items
if __name__ == "__main__":
fptr = open(os.environ['OUTPUT_PATH'], 'w')
S = ""
opStack = Stack()
n = int(input())
for i in range(n):
query = list(map(str, input().rstrip().split()))
if query[0] == '1': # Append
opStack.push(S)
S += query[1]
elif query[0] == '2': # Delete
opStack.push(S)
S = S[:-int(query[1])]
elif query[0] == '3': # Print
fptr.write(str(S[int(query[1])-1]) + '\n')
elif query[0] == '4': # Undo
S = opStack.pop()
fptr.close()