-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlogic.py
60 lines (46 loc) · 1.15 KB
/
logic.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
def createStack():
stack = []
return stack
def isEmpty(stack):
return len(stack)==0
def push(stack,x):
stack.append(x)
def pop(stack):
if isEmpty(stack):
#print("Error underflow")
else:
return stack.pop()
def printNSE(arr):
s = createStack()
dict={}
element = 0
next = 0
push(s,arr[0])
for i in range(1,len(arr),1):
next = arr[i];
if isEmpty(s) == False:
element = pop(s)
while element > next:
dict[element] = next
#print(str(element)+" ---> "+str(next),end=" ")
if isEmpty(s) == True:
break
element = pop(s)
if element < next:
push(s,element)
push(s,next)
while isEmpty(s) == False:
element = pop(s)
next = -1
dict[element] = -1
#print(str(element)+" ---> "+str(next),end=" ")
for i in range(n):
print(str(dict[arr[i]]), end=" ")
print("")
t = int(input())
for i in range(t):
list = []
n = int(input())
for i in range(n):
list.append(int(input()))
printNSE(list);