2) The function below implements a postfix calculator that takes an input equation as a character string with items sepa

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

2) The function below implements a postfix calculator that takes an input equation as a character string with items sepa

Post by answerhappygod »

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 + -"))
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply