Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Some examples:

[“2”, “1”, “+”, “3”, ““] -> ((2 + 1)  3) -> 9
[“4”, “13”, “5”, “/“, “+”] -> (4 + (13 / 5)) -> 6

class Solution:

# @param tokens, a list of string
# @return an integer
def add( self, num1, num2 ):
    return num1 + num2

def sub( self, num1, num2 ):
    return num1 - num2

def mul( self, num1, num2 ):
    return num1 * num2

def div( self, num1, num2 ):
    if num1 * num2 < 0:
        return -((-num1) / num2 )        
    return num1 / num2  
operator = { "+": add, "-": sub, "*": mul, "/": div }    

def evalRPN(self, tokens):
    nums = []
    for s in tokens:
      if s in ["+","-","*","/"]:
        num2 = nums.pop()
        num1 = nums.pop()
        re = self.operator.get(s)(self,num1,num2)          
        nums.append(re)
      else:
        nums.append(int(s))    
    return nums[0]</pre>