-
Notifications
You must be signed in to change notification settings - Fork 0
/
10866.py
40 lines (39 loc) · 905 Bytes
/
10866.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
import sys
from collections import deque
n = int(sys.stdin.readline().strip())
queue = deque([])
for i in range(n):
N = sys.stdin.readline().strip()
if N[:10] == "push_front":
N = N.split()
queue.appendleft(int(N[1]))
elif N[:9] == "push_back":
N = N.split()
queue.append(int(N[1]))
elif N == "pop_front":
try:
print(queue.popleft())
except:
print(-1)
elif N == "pop_back":
try:
print(queue.pop())
except:
print(-1)
elif N == "size":
print(len(queue))
elif N == "empty":
if len(queue)==0:
print(1)
else:
print(0)
elif N == "front":
try:
print(queue[0])
except:
print(-1)
elif N == "back":
try:
print(queue[-1])
except:
print(-1)