Question: 1 ) Create a class ArraySortedBag as a subclass of the class ArrayBag and define methods: constructor, _ _ add _ _ , find, add,

1) Create a class ArraySortedBag as a subclass of the class ArrayBag and define methods: constructor,
__add__, find, add, remove, __contain__, and merge.
2) Create a class LinkedSortedBag as a subclass of the class LinkedBag and define methods:
constructor, __add__, find, add, remove, __contain__, and merge.
3) Provide a tester function to thoroughly test these two classes (create instances and call all the
methods of these 2 classes)
"""
File: arraybag.py
"""
from arrays import Array
class ArrayBag(object):
"""An array-based bag implementation."""
# Class variable
DEFAULT_CAPACITY =10
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self.items = Array(ArrayBag.DEFAULT_CAPACITY)
self.size =0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def isEmpty(self):
"""Returns True if len(self)==0, or False otherwise."""
return len(self)==0
def __len__(self):
"""Returns the number of items in self."""
return self.size
def __str__(self):
"""Returns the string representation of self."""
return "{"+",".join(map(str, self))+"}"
def __iter__(self):
"""Supports iteration over a view of self."""
cursor =0
while cursor < len(self):
yield self.items[cursor]
cursor +=1
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
result = ArrayBag(self)
for item in other:
result.add(item)
return result
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise."""
# Exercise
return True
# Mutator methods
def clear(self):
"""Makes self become empty."""
# Exercise
pass
def add(self, item):
"""Adds item to self."""
# Check array memory here and increase it if necessary
# Exercise
self.items[len(self)]= item
self.size +=1
def remove(self, 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 Array(object):
"""Represents an array."""
def __init__(self, capacity, fillValue = None):
"""Capacity is the static size of the array.
fillValue is placed at each position."""
self._items = list()
for count in range(capacity):
self._items.append(fillValue)
def __len__(self):
"""-> The capacity of the array."""
return len(self._items)
def __str__(self):
"""-> The string representation of the array."""
return str(self._items)
def __iter__(self):
"""Supports iteration over a view of an array."""
return iter(self._items)
def __getitem__(self, index):
"""Subscript operator for access at index."""
return self._items[index]
def __setitem__(self, index, newItem):
"""Subscript operator for replacement at index."""
self._items[index]= newItem
"""
File: node.py
"""
class Node(object):
"""Represents a singly linked node."""
def __init__(self, data, next = None):
self.data = data
self.next = next
"""
File: baginterface.py
Author: Ken Lambert
"""
class BagInterface(object):
"""Interface for all bag types."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
pass
# Accessor methods
def isEmpty(self):
"""Returns True if len(self)==0, or False otherwise."""
return True
def __len__(self):
"""-Returns the number of items in self."""
return 0
def __str__(self):
"""Returns the string representation of self."""
return ""
def __iter__(self):
"""Supports iteration over a view of self."""
return None
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
return None
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise."""
return False
# Mutator methods
def clear(self):
"""Makes self become empty."""
pass
def add(self, item):
"""Adds item to self."""
pass
def remove(self, 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!