Created
August 18, 2014 10:09
-
-
Save entertainyou/2ceb019222e3381c109a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
class Solution: | |
def isOp(self, token): | |
return token in ["+", "-", "*", "/"] | |
def add(self, op1, op2): | |
return op1 + op2 | |
def minus(self, op1, op2): | |
return op1 - op2 | |
def multiply(self, op1, op2): | |
return op1 * op2 | |
def divide(self, op1, op2): | |
return op1 / op2 | |
# @param tokens, a list of string | |
# @return an integer | |
def evalRPN(self, tokens): | |
l = [] | |
op_map = { | |
"+": self.add, | |
"-": self.minus, | |
"*": self.multiply, | |
"/": self.divide, | |
} | |
for token in tokens: | |
if (self.isOp(token)): | |
op2 = l.pop() | |
op1 = l.pop() | |
func = op_map[token] | |
val = func(op1, op2) | |
print "val", val, op1, op2 | |
l.append(val) | |
else: | |
l.append(int(token)) | |
return l.pop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment