Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Submit a single file that contains the solutions to the two problems below. When you are finished, test your solutions using doctest. Include the following

Submit a single file that contains the solutions to the two problems below. When you are finished, test your solutions using doctest. Include the following code at the bottom of your module in order to run the doctest:
if __name__=='__main__':
import doctest
print( doctest.testfile('hw9TEST.py'))
1. Inheritance (based on 8.38) You MUST use inheritance for this problem. A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack. A stack is often referred to as a last-in first-out (LIFO) container because the last item inserted is the first removed. Implement a Stack class using inheritance. Note that this means you may be able to inherit some of the methods below. Which ones? (Try not writing those and see if it works!)
a. Constructor/_init__- Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top.
b. push() take an item as input and push it on the top of the stack
c. pop() remove and return the item at the top of the stack
d. isEmpty() returns True if the stack is empty, False otherwise
e.[] return the item at a given location, [0] is at the bottom of the stack
f. len() return length of the stack
The object is to make this client code work:
'''
>>> s = Stack()
>>> s.push('apple')
>>> s
Stack(['apple'])
>>> s.push('pear')
>>> s.push('kiwi')
>>> s
Stack(['apple', 'pear', 'kiwi'])
>>> top = s.pop()
>>> top
'kiwi'
>>> s
Stack(['apple', 'pear'])
>>> len(s)
2
>>> s.isEmpty()
False
>>> s.pop()
'pear'
>>> s.pop()
'apple'
>>> s.isEmpty()
True
>>> s = Stack(['apple', 'pear', 'kiwi'])
>>> s = Stack(['apple', 'pear', 'kiwi'])
>>> s[0]
'apple'
>>>
'''

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions