-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced_paranthesis.py
More file actions
37 lines (30 loc) · 1.01 KB
/
balanced_paranthesis.py
File metadata and controls
37 lines (30 loc) · 1.01 KB
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
def paranthesis(s):
while len(s)!=0:
s=s.replace("()","")
s=s.replace("[]","")
s=s.replace("{}","")
print(s)
if len(s)==0:
return True
return False
s=input("enter the paranthesis: ")
print(paranthesis(s))
def is_balanced(expression):
stack = []
opening_symbols = '({['
closing_symbols = ')}]'
for char in expression:
if char in opening_symbols:
stack.append(char)
elif char in closing_symbols:
if not stack:
return False # No matching opening symbol
last_opening = stack.pop()
if opening_symbols.index(last_opening) != closing_symbols.index(char):
return False # Mismatched opening and closing symbols
return not stack # If stack is empty, all symbols are balanced
# Example usage:
expression1 = "({[()]})"
expression2 = "{[({[)})"
print("Expression 1 is balanced:", is_balanced(expression1))
print("Expression 2 is balanced:", is_balanced(expression2))