-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0902.py
More file actions
43 lines (42 loc) · 1.13 KB
/
0902.py
File metadata and controls
43 lines (42 loc) · 1.13 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
# 연습문제 1 - 후위 표기법
# T = int(input())
# for tc in range(1, T + 1):
# problem = input()
# stack = []
# answer = []
# for i in problem:
# if i.isdigit():
# answer.append(i)
# else:
# stack.append(i)
# while stack:
# a = stack.pop()
# answer.append(a)
# print(f'#{tc} {"".join(answer)}')
# 연습문제 1 - Extra
T = int(input())
operator = {
'+': 0, '-': 0, '*': 1, '/': 1
}
for tc in range(1, T + 1):
problem = input()
stack = []
answer = []
for i in problem:
if i.isdigit():
answer.append(i)
else:
if stack and operator[i] > operator[stack[-1]]:
stack.append(i)
elif i == ')':
while stack[-1] != '(':
stack.pop()
stack.pop()
elif stack:
while stack and stack[-1] != '(' and i != '(' and operator[i] <= operator[stack[-1]]:
answer.append(stack.pop())
stack.append(i)
else:
stack.append(i)
answer += stack
print(*answer)