forked from zhuli19901106/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression-evaluation(AC).py
61 lines (50 loc) · 1.72 KB
/
expression-evaluation(AC).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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# There're some illegal cases, it's unfair
import re
class Solution:
# Define the precedence of operators
__pre = {}
__fun = {}
def __init__(self):
pre_count = -1
self.__pre['('] = pre_count
pre_count += 1
self.__pre['+'] = pre_count
self.__pre['-'] = pre_count
pre_count += 1
self.__pre['*'] = pre_count
self.__pre['/'] = pre_count
self.__pre['%'] = pre_count
pre_count += 1
self.__fun['+'] = lambda x, y: x + y
self.__fun['-'] = lambda x, y: x - y
self.__fun['*'] = lambda x, y: x * y
self.__fun['/'] = lambda x, y: x / y
self.__fun['%'] = lambda x, y: x % y
# @param expression: a list of strings;
# @return: an integer
def evaluateExpression(self, expression):
if (len(expression) == 0):
return 0
num = []
op = []
for token in expression:
if re.match(r'[+\-]?\d+', token):
num.append(int(token))
elif token == '(':
op.append(token)
elif token == ')':
while op[-1] != '(':
self.__calculate(num, op)
op.pop()
else:
while len(op) > 0 and self.__pre[op[-1]] >= self.__pre[token]:
self.__calculate(num, op)
op.append(token)
while len(op) > 0:
self.__calculate(num, op)
return num.pop() if len(num) > 0 else 0
def __calculate(self, num, op):
n2 = num.pop()
n1 = num.pop()
single_op = op.pop()
num.append(self.__fun[single_op](n1, n2))