forked from geekbuti/Hackerrank-solution-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-text-editor.py
28 lines (25 loc) · 929 Bytes
/
simple-text-editor.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
#!/usr/bin/env python3
if __name__ == "__main__":
ops_cnt = int(input().strip())
current = ''
history = []
for _ in range(ops_cnt):
args = input().strip().split(' ')
if args[0] == '1':
current += args[1]
history.append([args[0], len(args[1])])
elif args[0] == '2':
deleted = current[-int(args[1]):]
#print("deleted = {}".format(deleted))
current = current[:-int(args[1])]
#print("current = {}".format(current))
history.append([args[0], deleted])
elif args[0] == '3':
#print(str("".join(current))[int(args[1]) - 1])
print(current[int(args[1]) - 1])
elif args[0] == '4':
undo = history.pop()
if undo[0] == '1':
current = current[:-int(undo[1])]
elif undo[0] == '2':
current += undo[1]