-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEditor.py
77 lines (68 loc) · 2.07 KB
/
TextEditor.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# # Enter your code here. Read input from STDIN. Print output to STDOUT
# def make_op(result, ops, cmd, _ops):
# a = cmd.split(' ')
# if a[0] == '1':
# [result.append(c) for c in a[1]]
# ops.append(cmd)
# elif a[0] == '2':
# ops.append(cmd)
# try:
# for c in range(int(a[1])):
# _ops.append(result.pop())
# except:
# pass
# elif a[0] == '3':
# print(result[int(a[1])-1])
# elif a[0] == '4':
# cmd = ops.pop()
# a = cmd.split(' ')
# if a[0] == '1':
# try:
# [result.pop() for c in a[1]]
# except:
# pass
# elif a[0] == '2':
# _ops = _ops[::-1]
# for i, c in enumerate(_ops):
# result.append(c)
# _ops.pop(i)
# # print(result, _ops)
# result = []
# ops = []
# _ops = []
# q = int(input())
# for i in range(q):
# cmd = input()
# make_op(result, ops, cmd, _ops)
class TextEditor:
def __init__(self):
self.current_str = []
self.stack = []
def append(self, s):
self.stack.append("".join(self.current_str))
self.current_str.extend(list(s))
def delete(self, k):
self.stack.append("".join(self.current_str))
for _ in range(k):
_ = self.current_str.pop()
def print(self, k):
print(self.current_str[k-1])
def undo(self):
self.current_str = list(self.stack.pop())
def command(self, s):
op = int(s[0])
# print(self.current_str, self.stack)
if op == 1:
self.append(s.split(" ")[-1])
elif op == 2:
self.delete(int(s.split(" ")[-1]))
elif op == 3:
self.print(int(s.split(" ")[-1]))
elif op == 4:
self.undo()
if __name__ == "__main__":
Q = int(input())
editor = TextEditor()
for _ in range(Q):
command = input()
editor.command(command)