-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_queue1_metal_pipe.py
More file actions
35 lines (27 loc) · 916 Bytes
/
stack_queue1_metal_pipe.py
File metadata and controls
35 lines (27 loc) · 916 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
arrangement = '()(((()())(())()))(())'
def solution(arrangement):
queue = list(arrangement)
answer, level = 0, 0
cur_token = queue.pop(0)
while queue:
look_a_head = queue.pop(0)
if cur_token == '(':
if look_a_head == ')':
answer += level
elif look_a_head == '(':
level += 1
else:
raise SyntaxError('invaild arrangement')
elif cur_token == ')':
if look_a_head == ')':
level -= 1
answer += 1
elif look_a_head == '(':
pass
else:
raise SyntaxError('invaild arrangement')
else:
raise SyntaxError('invaild arrangement')
cur_token = look_a_head
return answer
print(solution(arrangement)) #17