-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpvalidator.py
More file actions
74 lines (59 loc) · 2.04 KB
/
expvalidator.py
File metadata and controls
74 lines (59 loc) · 2.04 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
############################
# Author: Hemant Tripathi #
###########################
class ExpValidator:
def __init__(self):
self.myarray = []
self.expression = ''
def push(self, element):
print('Stack Size: ', len(self.myarray));
print('Pushed data: ', element)
if(len(element) > 0):#insert element
self.myarray.append(element)
def pop(self, element):
popElement = self.myarray[len(self.myarray)-1]
if (element == ')' and popElement == '(') or (element == '}' and popElement == '{') or (element == ']' and popElement == '['):
print('Popped element: ', popElement)
self.myarray.pop()
return True
else:
print('Closing brace does not match with open brace. Cannot popped')
return False
def peek(self):
print('Stack peek element: ', self.myarray[len(self.myarray)-1])
def isEmpty(self):
if(len(self.myarray) <= 0):
return True
else:
return False
def stacksize(self):
return len(self.myarray)
def split(self, expression):
return list(expression)
def main():
print('------------------------------Expression Validator start -------------------------------------')
expValidator = ExpValidator()
expression = input('Enter the expression to validate: ')
tokens = expValidator.split(expression)
result = True
for token in tokens:
print('next token: ', token)
if token == '(' or token == '{' or token == '[':
expValidator.push(token)
if token == ')' or token == '}' or token == ']':
result = expValidator.pop(token)
if(result == True):
continue
else:
result = False
break
if result == True and expValidator.stacksize() == 0:
print('Valid Expression')
else:
print('Invalid Expression')
del expValidator
del expression
del result
del token
if __name__ == "__main__":
main()