# Python solution with detailed explanation

• Solution

Evaluate Reverse Polish Notation https://leetcode.com/problems/evaluate-reverse-polish-notation/

Postfix to Evaluation

• (2+5)4 = 2,5,+,4,
• Operand: Push on the stack
• Operator: pop 2 from stack and apply operation.
• Corner case = division. -2/3? Python integer division with negative numbers gives weird results. Safest to use will be int(x/float(y))
``````class Solution(object):
def process(self, token, op1, op2):
eval_methods = {
"+": lambda x, y: x+y,
"-": lambda x, y: x-y,
"*": lambda x, y: x*y,
"/": lambda x, y: int(x/float(y)),
}
return eval_methods[token](op1, op2)

def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
st = []
for token in tokens:
if token in set(["+", "-", "*", "/"]):
op2 = st.pop()
op1 = st.pop()
result = self.process(token, op1, op2)
st.append(result)
else:
st.append(int(token))
return st.pop()
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.