-
Notifications
You must be signed in to change notification settings - Fork 43
/
evaluate-reverse-polish-notation.py
355 lines (310 loc) · 10.8 KB
/
evaluate-reverse-polish-notation.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""
150. Evaluate Reverse Polish Notation
Medium
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, and /. Each operand may be an integer or another expression.
Note that division between two integers should truncate toward zero.
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
1 <= tokens.length <= 104
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].
"""
# V0
# IDEA : STACK + eval
# https://blog.csdn.net/fuxuemingzhu/article/details/79559703
class Solution(object):
def evalRPN(self, tokens):
stack = []
operators = ['+', '-', '*', '/']
for token in tokens:
if token not in operators:
stack.append(token)
else:
b = stack.pop()
a = stack.pop()
if token == '/' and int(a) * int(b) < 0:
res = eval('-' + '(' + '-' + a + '/' + b + ')')
else:
res = eval(a + token + b)
stack.append(str(res))
return int(stack.pop())
# V0'
# IDEA : STACK
# DEMO : call lambda via dict
# In [13]: ops = {
# ...: '+' : lambda y, x: x+y,
# ...: '-' : lambda y, x: x-y,
# ...: '*' : lambda y, x: x*y,
# ...: '/' : lambda y, x: int(x/y)
# ...: }
# ...:
# ...: a = 3
# ...: b = 4
# ...:
# ...:
# ...: for key in ops.keys():
# ...: print (ops[key])
# ...: print (ops[key](a,b))
# ...:
# <function <lambda> at 0x7f8c068c4730>
# 7
# <function <lambda> at 0x7f8c068c4048>
# 1
# <function <lambda> at 0x7f8c068c4a60>
# 12
# <function <lambda> at 0x7f8c068c48c8>
# 1
class Solution:
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
if len(tokens) < 1:
return None
ops = {
'+' : lambda y, x: x+y,
'-' : lambda y, x: x-y,
'*' : lambda y, x: x*y,
'/' : lambda y, x: int(x/y)
}
result = []
for token in tokens:
if token in ops.keys():
result.append(ops[token](result.pop(), result.pop()))
else:
result.append(int(token))
return result[0]
# V1
# IDEA : Reducing the List In-place
# https://leetcode.com/problems/evaluate-reverse-polish-notation/solution/
def evalRPN(self, tokens: List[str]) -> int:
operations = {
"+": lambda a, b: a + b,
"-": lambda a, b: a - b,
"/": lambda a, b: int(a / b),
"*": lambda a, b: a * b
}
current_position = 0
while len(tokens) > 1:
# Move the current position pointer to the next operator.
while tokens[current_position] not in "+-*/":
current_position += 1
# Extract the operator and numbers from the list.
operator = tokens[current_position]
number_1 = int(tokens[current_position - 2])
number_2 = int(tokens[current_position - 1])
# Calculate the result to overwrite the operator with.
operation = operations[operator]
tokens[current_position] = operation(number_1, number_2)
# Remove the numbers and move the pointer to the position
# after the new number we just added.
tokens.pop(current_position - 2)
tokens.pop(current_position - 2)
current_position -= 1
return tokens[0]
# V1
# IDEA : Reducing the List In-place + NO lambda functions.
# https://leetcode.com/problems/evaluate-reverse-polish-notation/solution/
def evalRPN(self, tokens: List[str]) -> int:
current_position = 0
while len(tokens) > 1:
# Move the current position pointer to the next operator.
while tokens[current_position] not in "+-*/":
current_position += 1
# Extract the operator and numbers from the list.
operator = tokens[current_position]
number_1 = int(tokens[current_position - 2])
number_2 = int(tokens[current_position - 1])
# Calculate the result to overwrite the operator with.
if operator == "+":
tokens[current_position] = number_1 + number_2
elif operator == "-":
tokens[current_position] = number_1 - number_2
elif operator == "*":
tokens[current_position] = number_1 * number_2
else:
tokens[current_position] = int(number_1 / number_2)
# Remove the numbers and move the pointer to the position
# after the new number we just added.
tokens.pop(current_position - 2)
tokens.pop(current_position - 2)
current_position -= 1
return tokens[0]
# V1
# IDEA : Evaluate with Stack
# https://leetcode.com/problems/evaluate-reverse-polish-notation/solution/
def evalRPN(self, tokens: List[str]) -> int:
operations = {
"+": lambda a, b: a + b,
"-": lambda a, b: a - b,
"/": lambda a, b: int(a / b),
"*": lambda a, b: a * b
}
stack = []
for token in tokens:
if token in operations:
number_2 = stack.pop()
number_1 = stack.pop()
operation = operations[token]
stack.append(operation(number_1, number_2))
else:
stack.append(int(token))
return stack.pop()
# V1
# https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/143004/Python-solution-O(n)-descriptive-solution
# https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/168183/Python-simple-functional-solution-(no-stack)
# IDEA : STACK
# STEPS:
# 1) lets create a dictionary hashmap of what needs to happen when an operator is encountered.
# 2) traverse through the tokens and catch all the numbers preceeding the operator.
# 3) pop the last two numbers -> perform the operation and store it in the stack.
# 4) keep repeating 2 and 3
# 5) you'll ultimately be left with the result in the stack
class Solution:
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
if len(tokens) < 1:
return None
ops = {
'+' : lambda y, x: x+y,
'-' : lambda y, x: x-y,
'*' : lambda y, x: x*y,
'/' : lambda y, x: int(x/y)
}
result = []
for token in tokens:
if token in ops.keys():
result.append(ops[token](result.pop(), result.pop()))
else:
result.append(int(token))
return result[0]
### Test case
s=Solution()
assert s.evalRPN([]) == None
assert s.evalRPN(["0"]) == 0
assert s.evalRPN(["-1"]) == -1
assert s.evalRPN(["-0"]) == 0
assert s.evalRPN(["-1","1","-"]) == -2
assert s.evalRPN(["0","1","+"]) == 1
assert s.evalRPN(["0","1","-"]) == -1
assert s.evalRPN(["2", "1", "+", "3", "*"]) == 9
assert s.evalRPN(["4", "13", "5", "/", "+"]) == 6
assert s.evalRPN(["1","1", "*"]) == 1
assert s.evalRPN(["1","1", "1", "*", "+"]) == 2
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79559703
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
operators = ['+', '-', '*', '/']
for token in tokens:
if token not in operators:
stack.append(token)
else:
b = stack.pop()
a = stack.pop()
if token == '/':
res = int(operator.truediv(int(a), int(b)))
else:
res = eval(a + token + b)
stack.append(str(res))
return int(stack.pop())
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79559703
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
operators = ['+', '-', '*', '/']
for token in tokens:
if token not in operators:
stack.append(token)
else:
b = stack.pop()
a = stack.pop()
if token == '/' and int(a) * int(b) < 0:
res = eval('-' + '(' + '-' + a + '/' + b + ')')
else:
res = eval(a + token + b)
stack.append(str(res))
return int(stack.pop())
# V1'''
# https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/47537/6-7-lines-in-Python
# IDEA : RECURSIVE
class Solution(object):
def evalRPN(self, tokens):
t = tokens.pop()
if t in '+-*/':
b = self.evalRPN(tokens)
a = self.evalRPN(tokens)
t = eval(a+t+b+'.')
return int(t)
# V1''''
# https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/47549/A-Python-solution-with-8-lines
class Solution(object):
def evalRPN(self, tokens):
stack = []
ops = {'+':lambda x, y: x+y, '-':lambda x, y: x-y, '*':lambda x, y: x*y, '/':lambda x, y: x/y}
for s in tokens:
try:
stack.append( float( s ) )
except:
stack.append( int( ops[s]( stack.pop(-2), stack.pop(-1) ) ) )
return int( stack[-1] )
# V1'''''
# https://leetcode.com/problems/evaluate-reverse-polish-notation/discuss/47537/6-7-lines-in-Python
# IDEA : ITERATION
class Solution(object):
def evalRPN(self, tokens):
stack = []
for t in tokens:
if t in '+-*/':
b, a = stack.pop(), stack.pop()
t = int(eval(a+t+b+'.'))
stack += t,
return int(stack[0])
# V2
# Time: O(n)
# Space: O(n)
import operator
class Solution(object):
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
numerals, operators = [], {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.div}
for token in tokens:
if token not in operators:
numerals.append(int(token))
else:
y, x = numerals.pop(), numerals.pop()
numerals.append(int(operators[token](x * 1.0, y)))
return numerals.pop()