-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced_Parentheses.py
More file actions
34 lines (32 loc) · 989 Bytes
/
Balanced_Parentheses.py
File metadata and controls
34 lines (32 loc) · 989 Bytes
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
class Stack:
def __init__(self):
self.items =[]
def push(self, value):
self.items.append(value)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def is_empty(self):
return len(self.items) == 0
def equation_checker(equation):
stack = Stack()
for letter in equation:
if len(equation) == 0:
print("Pass an equaltion")
return False
if letter == "(":
stack.push("(")
elif letter == ")":
if stack.size() == 0:
print("Unbalanced equation")
return False
stack.pop()
if(stack.size() == 0):
print("Balanced equation")
return True
else:
print("Unbalanced equation")
return False
print ("Pass" if (equation_checker('((3^2 + 8)*(5/2))/(2+6)')) else "Fail")
print ("Pass" if not (equation_checker('((3^2 + 8)*(5/2))/(2+6))')) else "Fail")