2) The function below implements a postfix calculator that takes an input equation as a character string with items sepa
Posted: Mon Mar 21, 2022 4:49 pm
2) The function below implements a postfix calculator that takes
an input equation as a character string with items separated by
spaces. Note that only integers and the + or – operators are
allowed.
def postfix_calc(s):
l = s.split()
m = []
for item in l:
if item ==
'+':
m.append(m.pop() + m.pop())
elif item ==
'-':
arg2 = m.pop()
arg1 = m.pop()
m.append(arg1 - arg2)
else:
m.append(int(item))
return m.pop()
What is the output after running the code below?
print(postfix_calc("3 2 + -"))
an input equation as a character string with items separated by
spaces. Note that only integers and the + or – operators are
allowed.
def postfix_calc(s):
l = s.split()
m = []
for item in l:
if item ==
'+':
m.append(m.pop() + m.pop())
elif item ==
'-':
arg2 = m.pop()
arg1 = m.pop()
m.append(arg1 - arg2)
else:
m.append(int(item))
return m.pop()
What is the output after running the code below?
print(postfix_calc("3 2 + -"))