-
Notifications
You must be signed in to change notification settings - Fork 0
/
DST_Stack.py
143 lines (110 loc) · 3.16 KB
/
DST_Stack.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class bugClass(object):
def __init__(self):
self.a = [1, 2]
print(self.a)
self.bug1()
def bug1(self):
b = self.a
b[0] = 0
print(b, self.a) # TODO: why self.a changes with b ?? How to FIX it ??
class stack():
def __init__(self):
self.stack_lst = []
def push(self, item):
return self.stack_lst.append(item)
def pop(self):
return self.stack_lst.pop()
def peek(self):
return self.stack_lst[-1]
def isEmpty(self):
return len(self.stack_lst) == 0
def size(self):
return len(self.stack_lst)
def min(self):
if self.stack_lst is not None:
return min(self.stack_lst)
else:
return None
def simpleTest():
s = stack()
print(s.isEmpty())
[s.push(i) for i in range(6)]
print(s.isEmpty(), s.size())
def parenthesesMatching(str_):
"""
USAGE: print('\n', parenthesesMatching('{(())(()()()())[]}'), '\n', parenthesesMatching('{(())(()()([)]())[]}'))
HTML/XML validation and operation
:param str_:
:return:
"""
def matches(a, b):
return '{[('.index(a) == '}])'.index(b)
s = stack()
for s_ in str_:
if s_ in '{[(':
s.push(s_)
else:
if s.isEmpty():
return 'not match!'
else:
if matches(s.peek(), s_):
s.pop()
if s.isEmpty():
return 'match!'
else:
return 'not match!'
def baseTransfer(num, base):
"""
USAGE: print(baseTransfer(25, 2), baseTransfer(25, 16))
convert any 10 base number to any base (!>16) number
:param num:
:param base:
:return:
"""
elem = '0123456789ABCDE'
s = stack()
while num > 0:
s.push(num % base)
num = num // base
num_new_str = ''
while not s.isEmpty():
num_new_str = num_new_str + elem[s.pop()]
return num_new_str
def suffixVal(eq_str):
storage = stack()
eq_lst = list(eq_str)
es_num_ = 0
for es in eq_lst:
try:
es_num = int(es)
storage.push(es_num)
except ValueError:
if es == '+':
a1 = storage.pop()
a2 = storage.pop()
es_num_new = a1 + a2
storage.push(es_num_new)
elif es == '*':
a1 = storage.pop()
a2 = storage.pop()
es_num_new = a1 * a2
storage.push(es_num_new)
elif es == '-':
a1 = storage.pop()
a2 = storage.pop()
es_num_new = a1 - a2
storage.push(es_num_new)
elif es == '/':
a1 = storage.pop()
a2 = storage.pop()
es_num_new = a1 / a2
storage.push(es_num_new)
return es_num_new
if __name__ == '__main__':
# print(suffixVal('34*5+'), suffixVal('345*+'), suffixVal('345*+6-'))
s = stack()
for xi in ["PSH3","MIN","PSH4","MIN","PSH2","MIN","PSH3","MIN","POP","MIN","POP","MIN","POP","MIN","PSH0","MIN"]:
s.push(xi)
print(s.min())
# bugClass()
pass