Day 18 (glamorized)

This commit is contained in:
Sebastian Seedorf
2020-12-19 00:48:12 +01:00
parent 51c3746627
commit af2e297a24
2 changed files with 6 additions and 10 deletions

View File

@@ -11,7 +11,7 @@ def calc(a, b, pop):
return operators[pop][1](a, b)
def pres(pop):
def prec(pop):
return operators.get(pop, [0])[0]
@@ -24,11 +24,11 @@ def evaluate(line):
elif char == '(':
stack.append(char)
elif char == ')':
while pres(stack[-1]) > 0:
while prec(stack[-1]) > 0:
postfix.append(calc(postfix.pop(), postfix.pop(), stack.pop()))
stack.pop() # remove (
elif char in "*+":
while pres(stack[-1]) > pres(char):
stack.pop()
elif char in operators:
while prec(stack[-1]) > prec(char):
postfix.append(calc(postfix.pop(), postfix.pop(), stack.pop()))
stack.append(char)
postfix.append(0)