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 + -"))
2) The function below implements a postfix calculator that takes an input equation as a character string with items sepa
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am