Skip to content

Commit 9380e47

Browse files
Balanced Parenthesis
1 parent 43f7a3b commit 9380e47

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Balanced_Parentheses.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Stack:
2+
def __init__(self):
3+
self.items =[]
4+
def push(self, value):
5+
self.items.append(value)
6+
def pop(self):
7+
return self.items.pop()
8+
def size(self):
9+
return len(self.items)
10+
def is_empty(self):
11+
return len(self.items) == 0
12+
13+
def equation_checker(equation):
14+
stack = Stack()
15+
for letter in equation:
16+
if len(equation) == 0:
17+
print("Pass an equaltion")
18+
return False
19+
if letter == "(":
20+
stack.push("(")
21+
elif letter == ")":
22+
if stack.size() == 0:
23+
print("Unbalanced equation")
24+
return False
25+
stack.pop()
26+
if(stack.size() == 0):
27+
print("Balanced equation")
28+
return True
29+
else:
30+
print("Unbalanced equation")
31+
return False
32+
33+
print ("Pass" if (equation_checker('((3^2 + 8)*(5/2))/(2+6)')) else "Fail")
34+
print ("Pass" if not (equation_checker('((3^2 + 8)*(5/2))/(2+6))')) else "Fail")

0 commit comments

Comments
 (0)