-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_brackets.py
More file actions
executable file
·43 lines (30 loc) · 1000 Bytes
/
check_brackets.py
File metadata and controls
executable file
·43 lines (30 loc) · 1000 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
42
43
# Check Brackets
# Author: jerrybelmonte
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, ch in enumerate(text):
if ch in "([{":
opening_brackets_stack.append(Bracket(ch, i + 1))
if ch in ")]}":
if not opening_brackets_stack:
opening_brackets_stack.append(Bracket(ch, i + 1))
break
if are_matching(opening_brackets_stack[-1].char, ch):
opening_brackets_stack.pop()
else:
opening_brackets_stack.append(Bracket(ch, i + 1))
break
return opening_brackets_stack
def main():
text = input()
mismatch = find_mismatch(text)
if mismatch:
print(mismatch.pop().position)
else:
print('Success')
if __name__ == "__main__":
main()