-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.py
79 lines (57 loc) · 1.78 KB
/
solution.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
78
79
ENQUEUE = 1 # Assign values to variables
DEQUEUE = 2
PRINT = 3
def read_command():
parts = input().strip().split(' ') # Taking input
cmd = int(parts[0])
if len(parts) == 1:
return (cmd, None)
try:
arg = int(parts[1])
except ValueError: # Handle error
arg = parts[1]
return cmd, arg
class Stack: # Create Stack class and define its methods
def __init__(self):
self._l = []
def push(self, data):
self._l.append(data)
def pop(self):
return self._l.pop()
def __len__(self):
return len(self._l)
def top(self):
if self._l:
return self._l[-1]
return None
class Queue: # Create Queue class and define its methods
def __init__(self):
self._head = Stack()
self._tail = Stack()
def enqueue(self, data):
self._tail.push(data)
def dequeue(self):
if self._head:
return self._head.pop()
return self._tail_to_head().pop()
def peek(self):
if self._head:
return self._head.top()
return self._tail_to_head().top()
def _tail_to_head(self):
while self._tail:
self._head.push(self._tail.pop())
return self._head
def main():
q = Queue()
n_commands = int(input().strip())
for _ in range(n_commands):
command, arg = read_command()
if command == ENQUEUE:
q.enqueue(arg)
elif command == DEQUEUE:
q.dequeue()
elif command == PRINT:
print(q.peek())
if __name__ == '__main__':
main()