Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[array_stack.py code is given below] class ArrayStack: LIFO Stack implementation using a Python list as underlying storage. def __init__(self): Create an empty stack. self._data =

image text in transcribed

[array_stack.py code is given below]

class ArrayStack: """LIFO Stack implementation using a Python list as underlying storage.""" def __init__(self): """Create an empty stack.""" self._data = [] # nonpublic list instance def __len__(self): """Return the number of elements in the stack.""" return len(self._data) def is_empty(self): """Return True if the stack is empty.""" return len(self._data) == 0 def push(self, e): """Add element e to the top of the stack.""" self._data.append(e) # new item stored at end of list def top(self): """Return (but do not remove) the element at the top of the stack. Raise Empty exception if the stack is empty. """ if self.is_empty(): raise Empty('Stack is empty') return self._data[-1] # the last item in the list def pop(self): """Remove and return the element from the top of the stack (i.e., LIFO). Raise Empty exception if the stack is empty. """ if self.is_empty(): raise Empty('Stack is empty') return self._data.pop() # remove last item from list if __name__ == '__main__': S = ArrayStack() # contents: [ ] S.push(5) # contents: [5] S.push(3) # contents: [5, 3] print(len(S)) # contents: [5, 3]; outputs 2 print(S.pop()) # contents: [5]; outputs 3 print(S.is_empty()) # contents: [5]; outputs False print(S.pop()) # contents: [ ]; outputs 5 print(S.is_empty()) # contents: [ ]; outputs True
Implement a program that can input an expression in postfix notation (see following explanation) and output its value, using stack from the class ArrayStack in file array_stack.py. Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if (expl)op(exp2) is a normal, fully parenthesized expression whose operation is op, the postfix version of this is pexp pexp2 op, where pexp, is the postfix version of exp, and pexp2 is the postfix version of exp2. The postfix version of a single number or variable is just that number or variable. For example, the postfix version of "((5+2)*(8-3))/4" is "52 +8 3 - *4/

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

Step: 3

blur-text-image

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899