Question: 1 ) Create a class ArraySortedBag as a subclass of the class ArrayBag and define methods: constructor, _ _ add _ _ , find, add,
Create a class ArraySortedBag as a subclass of the class ArrayBag and define methods: constructor,
add find, add, remove, contain and merge.
Create a class LinkedSortedBag as a subclass of the class LinkedBag and define methods:
constructor, add find, add, remove, contain and merge.
Provide a tester function to thoroughly test these two classes create instances and call all the
methods of these classes
File: arraybag.py
from arrays import Array
class ArrayBagobject:
An arraybased bag implementation."""
# Class variable
DEFAULTCAPACITY
# Constructor
def initself sourceCollection None:
Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self.items ArrayArrayBagDEFAULTCAPACITY
self.size
if sourceCollection:
for item in sourceCollection:
self.additem
# Accessor methods
def isEmptyself:
Returns True if lenself or False otherwise."""
return lenself
def lenself:
Returns the number of items in self."""
return self.size
def strself:
Returns the string representation of self."""
return joinmapstr self
def iterself:
Supports iteration over a view of self."""
cursor
while cursor lenself:
yield self.itemscursor
cursor
def addself other:
Returns a new bag containing the contents
of self and other."""
result ArrayBagself
for item in other:
result.additem
return result
def eqself other:
Returns True if self equals other,
or False otherwise."""
# Exercise
return True
# Mutator methods
def clearself:
Makes self become empty."""
# Exercise
pass
def addself item:
Adds item to self."""
# Check array memory here and increase it if necessary
# Exercise
self.itemslenself item
self.size
def removeself item:
Precondition: item is in self.
Raises: KeyError if
File: arrays.py
An Array is a restricted list whose clients can use
only len, iter, and str
class Arrayobject:
Represents an array."""
def initself capacity, fillValue None:
Capacity is the static size of the array.
fillValue is placed at each position."""
self.items list
for count in rangecapacity:
self.items.appendfillValue
def lenself:
The capacity of the array."""
return lenselfitems
def strself:
The string representation of the array."""
return strselfitems
def iterself:
Supports iteration over a view of an array."""
return iterselfitems
def getitemself index:
Subscript operator for access at index."""
return self.itemsindex
def setitemself index, newItem:
Subscript operator for replacement at index."""
self.itemsindex newItem
File: node.py
class Nodeobject:
Represents a singly linked node."""
def initself data, next None:
self.data data
self.next next
File: baginterface.py
Author: Ken Lambert
class BagInterfaceobject:
Interface for all bag types."""
# Constructor
def initself sourceCollection None:
Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
pass
# Accessor methods
def isEmptyself:
Returns True if lenself or False otherwise."""
return True
def lenself:
Returns the number of items in self."""
return
def strself:
Returns the string representation of self."""
return
def iterself:
Supports iteration over a view of self."""
return None
def addself other:
Returns a new bag containing the contents
of self and other."""
return None
def eqself other:
Returns True if self equals other,
or False otherwise."""
return False
# Mutator methods
def clearself:
Makes self become empty."""
pass
def addself item:
Adds item to self."""
pass
def removeself item:
Precondition: item is in self.
Raises: KeyError if item in not in self.
Postcondition: item is removed from self."""
pass
PLEASE WRITE ONE Python code that fulfills the all requirements in the questions and provide the output""
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
