-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1077.py
More file actions
43 lines (36 loc) · 848 Bytes
/
1077.py
File metadata and controls
43 lines (36 loc) · 848 Bytes
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
prec1 = "+-"
prec2 = "*/"
prec3 = "^"
def get_prec(op):
if op in prec1:
return 1
if op in prec2:
return 2
if op in prec3:
return 3
if op == ')' or op == '(':
return -1
return 0
def toPosfixa(entrada):
pilha = []
saida = ''
for i in entrada:
if get_prec(i) == 0:
saida += i
elif i == '(' :
pilha.append(i)
elif i == ')':
while pilha and pilha[-1] != '(':
saida += pilha.pop()
pilha.pop()
else:
while pilha and get_prec(pilha[-1]) >= get_prec(i):
saida += pilha.pop()
pilha.append(i)
while pilha:
saida += pilha.pop()
return saida
casos = int(input())
for i in range(casos):
entrada = input()
print(toPosfixa(entrada))