-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
balanced_parantheses.py
42 lines (32 loc) · 1.13 KB
/
balanced_parantheses.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
# A small check for balanced parantheses in Python 3
# Added regex as a little extra
import re
any_parantheses = re.compile("[\[({})\]]+")
opening_parantheses = ['[', '(', '{']
closing_parantheses = [']', ')', '}']
def is_balanced(expr):
stack = []
n = len(expr)
for i in range(0, n):
if (expr[i] in opening_parantheses):
stack.append(expr[i])
elif (expr[i] in closing_parantheses):
matching_paranthesis = opening_parantheses[closing_parantheses.
index(expr[i])]
if (stack.pop() != matching_paranthesis):
return False
return True if len(stack) == 0 else False
def main():
expr = input(
"Please enter an expression with parantheses ('[','(','{','}',')',']') or 'test' for a fixed test: "
)
if (expr == "test"):
expr = "({}[](([{()}])))"
if (not any_parantheses.match(expr)):
print("No parantheses found!")
elif (is_balanced(expr)):
print("%s is balanced!" % expr)
else:
print("%s is not balanced!" % expr)
if __name__ == '__main__':
main()