Day 18 (glamorized)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user