Skip to content

Commit

Permalink
use lambda expression
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Oct 28, 2018
1 parent f629be7 commit be49e36
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lc150. Evaluate Reverse Polish Notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ def evalRPN(self, tokens):
"""
operators = {'+', '-', '*', '/'}
oprd = []
# use lambda expression
ops = {
'*': lambda x, y: x * y,
'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
# int() trunc to 0
'/': lambda x, y: int(x / y),
}

for tk in tokens:
if tk in operators:
b = oprd.pop()
a = oprd.pop()
if tk == '+':
c = a + b
elif tk == '-':
c = a - b
elif tk == '*':
c = a * b
else:
c = int(a / b)
oprd.append(c)
# no need to pop another operand
oprd[-1] = ops[tk](oprd[-1], b)
else:
oprd.append(int(tk))
return oprd[0]
Expand Down

0 comments on commit be49e36

Please sign in to comment.