Question: 4.3) Can you please help me with this Python question. The grow() and shrink() methods should use the strategies discussed in this chapter to increase
4.3) Can you please help me with this Python question.
The grow() and shrink() methods should use the strategies discussed in this chapter to increase or decrease the length of the list contained in the array.
Be sure to reuse your solution from Programming Exercise 4.2 as your starter file for the arrays.py file. ( I will post 4.2 solution )
In the Array class of the arrays.py file complete the following:
- Define the grow() method.
- Add the fillValue to the new cells in the underlying list
- Each call to grow() should double the physical size.
- Make sure that the arrays cells use the fillValue when the arrays size is increased.
2 Define the shrink() method.
- Shrink the size by half but not below the default capacity
- Remove those garbage cells from the underlying list
- Make sure that the physical size of the array does not shrink below the user-specified capacity.
To test your program run the main() method below in the arrays.py file:
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()
Your program's output should look like the following:
Physical size: 5 Logical size: 0 Items: [None, None, None, None, None] Items: [None, None, None, None, None, None, None, None, None, None] Items: [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] Items: [None, None, None, None, None, None, None, None, None, None] Items: [None, None, None, None, None] Items: [None, None, None, None, None]
File: arrays.py
"""
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
The fill value is None by default.
Reuse your solution from Programming Exercise 4.2 as your starter file
"""
class Array(object):
"""Represents an array."""
# Reuse your solution from Programming Exercise 4.2 as your starter file
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 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()
4.2 Solution
"""
File: arrays.py
An Array is a restricted list whose clients can use
only [], len, iter, and str.
To instantiate, use
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()
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):
# Add the
# precondition.
if not (index >=0 and index raise IndexError """Subscript operator for access at index.""" return self.items[index] def __setitem__(self, index, newItem): # Add the # precondition. if not (index >=0 and index raise IndexError """Subscript operator for replacement at index.""" self.items[index] = newItem # Define the # size function(). def size(self): count = 0 # Run the loop to # traverse the array. for i in self.items: # If the current element # is None, return the count if i == None: return count # Increase the # count by 1. count += 1 return count def main(): """Test code for modified Array class.""" a = Array(10) print("Physical size:", len(a)) print("Logical size:", a.size()) print("Items:", a) print(a[0]) if __name__ == "__main__": main() Here is what i have done. When you remove last main part, GROW works, but when main is there both doesn't work. class Array(object): def __init__(self, capacity, fillValue = None): self.items = list() self.logicalSize = 0 self.capacity = capacity self.fillValue = fillValue for count in range(capacity): self.items.append(fillValue) def __len__(self): return len(self.items) def __str__(self): return str(self.items) def __iter__(self): return iter(self.items) def __getitem__(self, index): if index < 0 or index >= self.size(): raise IndexError("Array index out of bounds") return self.items[index] def __setitem__(self, index, newItem): if index < 0 or index >= self.size(): raise IndexError("Array index out of bounds") self.items[index] = newItem def size(self): return self.logicalSize def grow(self): temp_array = list() for i in range(len(self.items)): temp_array.append(self.items[i]) self.items = temp_array for count in range(len(self.items), len(self.items) * 2): self.items.append(self.fillValue) def shrink(self): if(self.capacity <= (len(self.items)/2)): temp_array = list() for i in range(len(self.items)//2): temp_array.append(self.items[i]) self.items = temp_array
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
