Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3 (use Stack to evaluate the postfix expression as shown in lecture slides) 1.Write a function that accepts a valid postfix expression and evaluates

Python 3

(use Stack to evaluate the postfix expression as shown in lecture slides)

1.Write a function that accepts a valid postfix expression and evaluates it.

CODE:

# Stack.py

class Stack:

#------------------------------------------------------------

def __init__(self):

'''post: creates an empty LIFO stack'''

self.items = []

#------------------------------------------------------------

def push(self, item):

'''post: places x on top of the stack'''

self.items.append(item)

#------------------------------------------------------------

def pop(self):

'''post: removes and returns the top element of

the stack'''

return self.items.pop()

#------------------------------------------------------------

def top(self):

'''post: returns the top element of the stack without

removing it'''

return self.items[-1]

#------------------------------------------------------------

def size(self):

'''post: returns the number of elements in the stack'''

return len(self.items)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions