-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_stack_n.py
30 lines (27 loc) · 920 Bytes
/
AC_stack_n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_stack_n.py
# Create Date: 2015-03-15 16:18:08
# Usage: AC_stack_n.py
# Descripton:
class Solution:
# @param tokens, a list of string
# @return an integer
def evalRPN(self, tokens):
stack = []
for str in tokens:
if str not in '+-*/':
tmp = int(str)
stack.append(tmp)
else:
a = stack.pop()
b = stack.pop()
if str == '+': stack.append(b + a)
elif str == '-': stack.append(b - a)
elif str == '*': stack.append(b * a)
elif str == '/': stack.append(int(b * 1.0 / a))
print b, str, a, stack[-1]
return stack[0]
s = Solution()
print s.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]) # 22