019f8fd211
git-subtree-dir: users/wpcarro git-subtree-mainline:464bbcb15c
git-subtree-split:24f5a642af
Change-Id: I6105b3762b79126b3488359c95978cadb3efa789
51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
operators = {
|
|
'*': 1,
|
|
'+': 0,
|
|
}
|
|
|
|
def tokenize(xs):
|
|
result = []
|
|
i = 0
|
|
while i < len(xs):
|
|
current = xs[i]
|
|
if current in operators.keys():
|
|
result.append(current)
|
|
i += 1
|
|
continue
|
|
else:
|
|
i += 1
|
|
while i < len(xs) and xs[i] in {str(n) for n in range(10)}:
|
|
current += xs[i]
|
|
i += 1
|
|
result.append(int(current))
|
|
return result
|
|
|
|
def postfix(xs):
|
|
result = []
|
|
s = []
|
|
for x in xs:
|
|
if x in operators.keys():
|
|
while s and operators[s[-1]] >= operators[x]:
|
|
result.append(s.pop())
|
|
s.append(x)
|
|
else:
|
|
result.append(x)
|
|
while s:
|
|
result.append(s.pop())
|
|
return result
|
|
|
|
def evaluate(xs):
|
|
s = []
|
|
for x in xs:
|
|
print(s, x)
|
|
if x == '*':
|
|
s.append(s.pop() * s.pop())
|
|
elif x == '+':
|
|
s.append(s.pop() + s.pop())
|
|
else:
|
|
s.append(x)
|
|
print(s)
|
|
return s[-1]
|
|
|
|
|
|
print(evaluate(postfix(tokenize("12+3*10"))))
|