-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparentheses.py
56 lines (41 loc) · 1.42 KB
/
parentheses.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
51
# Copyright (C) Deepali Srivastava - All Rights Reserved
# This code is part of DSA course available on CourseGalaxy.com
from StackArray import Stack
def is_valid(expr):
st = Stack()
for ch in expr:
if ch in '({[':
st.push(ch)
if ch in ')}]':
if st.is_empty():
print("Right parentheses are more than left parentheses")
return False
else:
char = st.pop()
if not match_parentheses(char,ch):
print("Mismatched parentheses are ", char , " and " , ch)
return False
if st.is_empty():
print("Balanced Parentheses")
return True
else:
print("Left parentheses are more than right parentheses")
return False
def match_parentheses(left_par, right_par):
if left_par == '[' and right_par == ']':
return True
if left_par == '{' and right_par == '}':
return True
if left_par == '(' and right_par == ')':
return True
return False
#############################################################################
while True:
print("Enter an expression with parentheses (q to quit) : ", end = ' ')
expression = input()
if expression == "q":
break
if is_valid(expression):
print("Valid expression")
else:
print("Invalid expression")