https://leetcode.com/problems/evaluate-reverse-polish-notation/


Success

___


```python
class Stack:
    # copid
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def push(self, items):
        self.items.append(items)

    def pop(self):
        return self.items.pop()

#Perform a basic arithmetic operation using +,-,*,/,^
def calculate(operator, operand1, operand2):
    # copid
    if operator == "+":
        return operand1 + operand2
    elif operator == "-":
        return operand1 - operand2
    elif operator == "*":
        return operand1 * operand2
    elif operator == "/":
        return int(operand1 / operand2)
    elif operator == "^": # exponentiation operator
        return operand1 ** operand2        

#Evaluate an RPN expression 
def postfixEval(expression):
    # copid
    operators = ["+","-","*","/","^"]
    operandStack = Stack()
    tokenList = expression.split(" ")
    
    for token in tokenList:
      #Check is the token is an operator or an operand!
      if token in operators: 
        operand2 = operandStack.pop()
        operand1 = operandStack.pop()
        result = calculate(token,operand1,operand2)
        operandStack.push(result)
      else: #Toke is an Operand
        operandStack.push(float(token))
        
    return operandStack.pop()


class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        #2022/12/20 5:57
        expression = " ".join(tokens)
        return int(postfixEval(expression))
        #2022/12/20 6:14
```
