-
Notifications
You must be signed in to change notification settings - Fork 74
/
check_brackets.py
48 lines (40 loc) · 1.35 KB
/
check_brackets.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
40
41
42
43
44
45
46
47
48
# python3
import sys
class Bracket:
def __init__(self, bracket_type, position):
self.bracket_type = bracket_type
self.position = position
def Match(self, c):
if self.bracket_type == '[' and c == ']':
return True
if self.bracket_type == '{' and c == '}':
return True
if self.bracket_type == '(' and c == ')':
return True
return False
if __name__ == "__main__":
text = sys.stdin.read()
opening_brackets_stack = []
result = True
for i, next in enumerate(text):
if next in ["(", "[", "{"]:
opening_brackets_stack.append(Bracket(next, i))
elif next in [")", "]", "}"]:
if not opening_brackets_stack:
result = i + 1
break
else:
bracket = opening_brackets_stack[-1]
result = bracket.Match(next)
if result:
del opening_brackets_stack[-1]
else:
result = i + 1
break
if result and isinstance(result, type(
True)) and not opening_brackets_stack:
print("Success")
elif result and isinstance(result, type(True)):
print(opening_brackets_stack[-1].position + 1)
else:
print(result)