Question: Can you please help me with this Python add() method question. Exercise 5.3 The add() method resizes the array properly when items are added to
Can you please help me with this Python add() method question.
Exercise 5.3
The add() method resizes the array properly when items are added to the array.
In the arraybag.py file define the add() method in the ArrayBag class by completing the following:
- Check array memory and increase it if necessary
To test your program run the test() method in the testbag.py file. ( i will post code for this too so you can test the code) This is to run test() / to test only.
Your program's output should look like the following:
Testing
The list of items added is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Expect the bag's string: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Add 5 more items to test increasing the array size:
Expect the bag's string: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
File: arraybag.py
"""
Project 5.3
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."""
if self is other: return True
if type(self) != type(other) or \
len(self) != len(other):
return False
for item in self:
if self.count(item) != other.count(item):
return False
return True
def count(self, item):
"""Returns the number of instances of item in self."""
total = 0
for nextItem in self:
if nextItem == item:
total += 1
return total
# Mutator methods
def clear(self):
"""Makes self become empty."""
self.size = 0
self.items = Array(ArrayBag.DEFAULT_CAPACITY)
def add(self, item):
"""Adds item to self."""
# Check array memory here and increase it if necessary
File: testbag.py # To test code
"""
File: testbag.py
A tester program for bag implementations.
"""
from arraybag import ArrayBag
from linkedbag import LinkedBag
def test(bagType):
"""Expects a bag type as an argument and runs some tests
on objects of that type."""
print("Testing", bagType)
lyst = list(range(1, 11))
print("The list of items added is:", lyst)
b = bagType(lyst)
print("Expect the bag's string:", b)
print("Add 5 more items to test increasing the array size:")
for i in range(11, 16):
b.add(i)
print("Expect the bag's string:", b)
test(ArrayBag)
#test(LinkedBag)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
