-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
75 lines (55 loc) · 1.36 KB
/
code.py
File metadata and controls
75 lines (55 loc) · 1.36 KB
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
'''
:param x: value to be inserted
:return: None
queue_1 = [] # first queue
queue_2 = [] # second queue
'''
def push(x):
# global declaration
global queue_1
global queue_2
queue_1.append(x)
def pop():
'''
:return: the value of top of stack and pop from it.
'''
# global declaration
global queue_1
global queue_2
while len(queue_1) > 1 :
queue_2.append(queue_1.pop(0))
element = -1
if len(queue_1) == 1 :
element = queue_1.pop(0)
while len(queue_2) > 0 :
queue_1.append(queue_2.pop(0))
return element
import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
queue_1 = [] # first queue
queue_2 = [] # second queue
if __name__ == '__main__':
test_cases = int(input())
for cases in range(test_cases) :
n = int(input())
a = list(map(int,input().strip().split()))
i = 0
while i<len(a):
if a[i] == 1:
push(a[i+1])
i+=1
else:
print(pop(),end=" ")
i+=1
# clear both the queues
queue_1 = []
queue_2 = []
print()