-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackExercise.py
More file actions
executable file
·263 lines (213 loc) · 5.95 KB
/
StackExercise.py
File metadata and controls
executable file
·263 lines (213 loc) · 5.95 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
def isBalancedParenthesis(expn):
stk = []
for ch in expn:
if ch == '{' or ch == '[' or ch == '(':
stk.append(ch)
elif ch == '}':
if stk.pop() != '{':
return False
elif ch == ']':
if stk.pop() != '[':
return False
elif ch == ')':
if stk.pop() != '(':
return False
return len(stk) == 0
def main1():
expn = "{()}[]"
value = isBalancedParenthesis(expn)
print "Given Expn:" , expn
print "Result after isParenthesisMatched:" , value
def insertAtBottom(stk, value): # !!!!!!!!! waste for python.
if len(stk) == 0:
stk.append(value)
else:
temp = stk.pop()
insertAtBottom(stk, value)
stk.append(temp)
def reverseStack(stk): # !!!!!!!!! waste for python.
if len(stk) == 0:
return
else:
value = stk.pop
reverseStack(stk)
insertAtBottom(stk, value)
def postfixEvaluate(expn):
stk = []
token_list = expn.split()
for token in token_list:
if token == '+':
num1 = stk.pop();
num2 = stk.pop();
stk.append(num1 + num2)
elif token == '-':
num1 = stk.pop();
num2 = stk.pop();
stk.append(num1 - num2)
elif token == '*':
num1 = stk.pop();
num2 = stk.pop();
stk.append(num1 * num2)
elif token == '/':
num1 = stk.pop();
num2 = stk.pop();
stk.append(num1 / num2)
else:
stk.append(int(token))
return stk.pop()
def main2():
expn = "6 5 2 3 + 8 * + 3 + *"
value = postfixEvaluate(expn)
print "Given Postfix Expn: " , expn
print "Result after Evaluation: " , value
def precedence(x):
if x == '(':
return (0)
if x == '+' or x == '-':
return (1)
if x == '*' or x == '/' or x == '%':
return (2)
if x == '^':
return (3)
return (4)
def infixToPostfix(expn):
stk = []
token_list = expn.split()
output = ""
for token in token_list:
if token in '+-*/^':
while len(stk) != 0 and precedence(token) <= precedence(stk[len(stk) - 1]):
outsrt = stk.pop()
output = output + " " + outsrt
stk.append(token)
elif token == '(':
stk.append(token)
elif token == ')':
outsrt = None
while len(stk) != 0 and outsrt != '(':
outsrt = stk.pop()
if outsrt != '(' :
output = output + " " + outsrt
else :
output = output + " " + token
while len(stk) != 0:
outsrt = stk.pop()
output = output + " " + outsrt
return output
def main3():
expn = "10 + ( ( 3 ) ) * 5 / ( 16 - 4 )"
# value = infixToPostfix(expn)
value = infixToPrefix(expn)
print "Infix Expn: " , expn
print "Postfix Expn: " , value
def infixToPrefix(expn):
expn = reverseString(expn)
expn = replaceParanthesis(expn)
expn = infixToPostfix(expn)
expn = reverseString(expn)
return expn
def replaceParanthesis(expn):
lower = 0
upper = len(expn)
newexp = ""
while lower < upper:
if expn[lower] == '(':
newexp += ')'
elif expn[lower] == ')':
newexp += '('
else:
newexp += expn[lower]
lower += 1
return newexp
def reverseString(expn):
lower = 0
upper = len(expn)
newexp = ""
while lower < upper:
newexp += expn[upper - 1]
upper -= 1
return newexp
def main4():
expn = "10+((3))*5/(16-4)"
value = infixToPrefix(expn)
print "Infix Expn: " , expn
print "Prefix Expn: " , value
def StockSpanRange(arr):
SR = [0] * len(arr)
SR[0] = 1
i = 1
size = len(arr)
while i < size:
SR[i] = 1
j = i - 1
while (j >= 0) and (arr[i] >= arr[j]):
SR[i] += 1
j -= 1
i += 1
return SR
def main5():
arr = [6, 5, 4, 3, 2, 4, 5, 7, 9]
value = StockSpanRange2(arr)
print "StockSpanRange: " , value
def StockSpanRange2(arr):
stk = []
size = len(arr)
SR = [0] * size
stk.append(0)
SR[0] = 1
i = 1
while i < size:
while len(stk) != 0 and arr[stk[len(stk) - 1]] <= arr[i]:
stk.pop()
if (len(stk) == 0):
SR[i] = i + 1
else:
SR[i] = i - stk[len(stk) - 1]
stk.append(i)
i += 1
return SR
def GetMaxArea(arr):
size = len(arr)
maxArea = -1
minHeight = 0
i = 1
while i < size:
minHeight = arr[i]
j = i - 1
while j >= 0:
if minHeight > arr[j]:
minHeight = arr[j]
currArea = minHeight * (i - j + 1)
if maxArea < currArea:
maxArea = currArea
j -= 1
i += 1
return maxArea
def GetMaxArea2(arr):
""" generated source for method GetMaxArea2 """
size = len(arr)
stk = []
maxArea = 0
i = 0
while i < size:
while (i < size) and (len(stk) == 0 or arr[stk[len(stk) - 1]] <= arr[i]):
stk.append(i)
i += 1
while not len(stk) == 0 and (i == size or arr[stk[len(stk) - 1]] > arr[i]):
top = stk[len(stk) - 1]
stk.pop()
topArea = arr[top] * (i if len(stk) == 0 else i - stk[len(stk) - 1] - 1)
if maxArea < topArea:
maxArea = topArea
return maxArea
def main6():
arr = [7, 6, 5, 4, 4, 1, 6, 3, 1]
value = GetMaxArea2(arr)
print "GetMaxArea: " , value
main1()
main2()
main3()
main4()
main5()
main6()