Question
Please follow the instructions and code provided in Python otherwise it will not work properly. Thank you. Code is provided below: File: arrays.py Project
Please follow the instructions and code provided in Python otherwise it will not work properly. Thank you.
Code is provided below:
"""
File: arrays.py
Project 4.3
Adds methods grow and shrink to increase or decrease the capacity
of the array if necessary.
An Array is a restricted list whose clients can use
only [], len, iter, and str.
To instantiate, use
= array(, )
The fill value is None by default.
"""
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()
self.logicalSize = 0
# Track the capacity and fill value for adjustments later
self.capacity = capacity
self.fillValue = fillValue
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 traversal with a for loop."""
return iter(self.items)
def __getitem__(self, index):
"""Subscript operator for access at index.
Precondition: 0
if index = self.size():
raise IndexError("Array index out of bounds")
return self.items[index]
def __setitem__(self, index, newItem):
"""Subscript operator for replacement at index.
Precondition: 0
if index = self.size():
raise IndexError("Array index out of bounds")
self.items[index] = newItem
def size(self):
"""-> The number of items in the array."""
return self.logicalSize
def grow(self):
"""Increases the physical size of the array if necessary."""
# Double the physical size if no more room for items
# and add the fillValue to the new cells in the underlying list
# your code here
def shrink(self):
"""Decreases the physical size of the array if necessary."""
# Shrink the size by half but not below the default capacity
# and remove those garbage cells from the underlying list
# your code here
def main():
"""Test code for modified Array class."""
a = Array(5)
print("Physical size:", len(a))
print("Logical size:", a.size())
print("Items:", a)
a.grow()
print("Items:", a)
a.grow()
print("Items:", a)
a.shrink()
print("Items:", a)
a.shrink()
print("Items:", a)
a.shrink()
print("Items:", a)
if __name__ == "__main__":
main()
Add the methods grow and shrink to the Array class. These methods should use the strategies discussed in this chapter to increase or decrease the length of the list contained in the array. Each call to .grow() should double the physical size. Make sure that the physical size of the array does not shrink below the user-specified capacity and that the array's cells use the fill value when the array's size is increased. A main() has been provided to test the implementation of grow and shrinkStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started