-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
81 lines (72 loc) · 2.42 KB
/
Copy pathstack.py
File metadata and controls
81 lines (72 loc) · 2.42 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
75
76
77
78
79
80
81
class Stack:
def __init__(self):
self.__data = []
self.__size = 0
def __len__(self):
return self.__size
def push(self, item):
self.__data.append(item)
self.__size += 1
def pop(self):
item = self.__data.pop()
self.__size -= 1
return item
def top(self):
return self.__data[-1]
def is_empty(self):
return self.__size == 0
# evaluate postfix expression using stack
# Postfix expression requires that its operators come after the corresponding operands
# e.g '22 3 5 * +' is equivalent of 3 * 5 + 22 => eval_postfix('22 3 5 * +') = 37
@classmethod
def eval_postfix(cls, expression):
s = cls()
for c in expression.split():
if c.isdigit():
s.push(int(c))
else:
b = s.pop()
a = s.pop()
if c == '*':
s.push(a * b)
elif c == '/':
s.push(a / b)
elif c == '+':
s.push(a + b)
elif c == '-':
s.push(a - b)
else:
raise ValueError('Illegal postfix expression')
return s.pop()
# Check balanced parentheses in an expression
@classmethod
def balanced_parentheses(cls, expression):
s = cls()
# same type of parentheses have same index
opening = '([{'
closing = ')]}'
for c in expression:
# encountered opening parentheses => push to the stack
if c in opening:
s.push(c)
# encountered closing parentheses
elif c in closing:
# there is no parentheses to match => invalid
if s.is_empty(): return False
last = s.pop()
# are not of the same type => invalid
if not opening.find(last) == closing.find(c):
return False
# all opening parentheses were matched with closing => valid
return s.is_empty()
# sort items using stack
# time complexity: O(n^2), space usage: O(n)
def sort(self):
tmp = Stack()
while not self.is_empty():
item = self.pop()
while not tmp.is_empty() and tmp.top() > item:
self.push(tmp.pop())
tmp.push(item)
self.__data = tmp.__data
return self