-
Notifications
You must be signed in to change notification settings - Fork 0
/
brackets.py
49 lines (38 loc) · 1.09 KB
/
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
49
import re
class Stack:
def __init__(self):
self.q = []
def insert(self, i):
self.q.append(i)
def peak(self):
return self.q[-1]
def pop(self):
last = self.peak()
self.q.pop()
return last
def length(self):
return len(self.q)
def is_balanced(s):
stack = Stack()
if re.match(r'[\[{(\]})]', s):
ref = {']': '[', '}': '{', ')': '('}
for c in s:
if re.match(r'[\[{(]', c):
# left braces match
stack.insert(c)
pass
elif re.match(r'[\]})]', c):
#
if (stack.length() > 0 and ref[c] == stack.peak()):
stack.pop()
else:
return False
return stack.length() == 0
if __name__ == "__main__":
# print(is_balanced('((5+3)*2+1)'))
print(is_balanced('[1+1]+(2*2)-{3/3}'))
print(is_balanced('(({[(((1)-2)+3)-3]/3}-3)'))
print(is_balanced('(3+{1-1)}'))
print(is_balanced('2+3'))
print(is_balanced('(((1+(1+1))))]'))
print(is_balanced(']'))