-
Notifications
You must be signed in to change notification settings - Fork 1
/
brackets_in_code.py
39 lines (31 loc) · 1.04 KB
/
brackets_in_code.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
from collections import namedtuple
Bracket = namedtuple("Bracket", ["char", "position"])
def are_matching(left, right):
return (left + right) in ["()", "[]", "{}"]
def find_mismatch(text):
opening_brackets_stack = []
for i, next in enumerate(text):
if next in "([{":
# Process opening bracket, write your code here
opening_brackets_stack.append(Bracket(next,i))
if next in ")]}":
# Process closing bracket, write your code here
if not opening_brackets_stack:
return Bracket(next,i)
top = opening_brackets_stack.pop()
if not are_matching(top.char, next):
return Bracket(next,i)
if not opening_brackets_stack:
return None
else:
return opening_brackets_stack[0]
def main():
text = input()
mismatch = find_mismatch(text)
# Printing answer, write your code here
if mismatch==None:
print("Success")
else:
print(mismatch.position+1)
if __name__ == "__main__":
main()