forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalanced_parentheses_checker.py
50 lines (41 loc) · 1.3 KB
/
Balanced_parentheses_checker.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
50
class Stack:
def __init__(self):
self.items = []
self.top = -1
def is_full(self):
return self.top == 99 # MAX_SIZE - 1
def is_empty(self):
return self.top == -1
def push(self, item):
if not self.is_full():
self.items.append(item)
self.top += 1
def pop(self):
if not self.is_empty():
self.top -= 1
return self.items.pop()
return None
def peek(self):
if not self.is_empty():
return self.items[self.top]
return None
def is_balanced(expression):
stack = Stack()
for ch in expression:
if ch in '({[':
stack.push(ch)
elif ch in ')}]':
if stack.is_empty():
return False # Unmatched closing parenthesis
top = stack.pop()
if (ch == ')' and top != '(') or \
(ch == '}' and top != '{') or \
(ch == ']' and top != '['):
return False # Mismatched parentheses
return stack.is_empty() # Check if stack is empty at the end
if __name__ == "__main__":
expression = input("Enter an expression: ")
if is_balanced(expression):
print("The parentheses are balanced.")
else:
print("The parentheses are not balanced.")