-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrackets.py
29 lines (27 loc) · 935 Bytes
/
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
def check(string):
# return success or index of wrong bracket
string = string.replace(' ', '')
match = {'(': ')', '{': '}', '[': ']'}
opens_b = ['[', '(', '{']
# list of pairs [bracket, index]
opens = []
for i, s in enumerate(string):
# открывающая скобка
if s in opens_b:
# кладем в список
opens.append([s, i])
else:
# есть открывающие скобки
if len(opens) > 0:
# совпадает с последней открывающей скобкой
if s != match[opens.pop()[0]]:
return i + 1
else:
return i + 1
if len(opens) == 0:
return 'Success'
else:
return opens[0][1] + 1
if __name__ == '__main__':
brackets = input('Input string of brackets\n')
print(check(brackets))