subject

Add the methods insert and pop to the Array class. These methods should use the strategies discussed in this chapter, including adjusting the length of the array, if necessary. The insert method expects a position and an item as arguments and inserts the item at the given position. If the position is greater than or equal to the arrayâs logical size, the method inserts the item after the last item currently available in the array. The pop method expects a position as an argument and removes and returns the item at that position. The pop methodâs precondition is 0 <= index < size(). The remove method should also reset the vacated array cell to the fill value. A main() has been provided to test the implementation of the insert and pop methods. File: arrays. py
"""
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 <= index < size()"""
if index < 0 or 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 <= index < size()"""
if index < 0 or 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
for count in range(len(self)):
self. items. append(self. fillValue)
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
newSize = max(self. capacity, len(self) // 2)
for count in range(len(self) - newSize):
self. items. pop()

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 00:30
At an open or uncontrolled intersection, yield if a. your road is paved and the crossroad is not b. the cross road is paved and yours is not c. you have two or more passengers in your vehicle d. you did not yield in the last intersection
Answers: 1
question
Computers and Technology, 22.06.2019 19:00
In he example code, what does the title attribute create? a tool tip an element a source a markup
Answers: 1
question
Computers and Technology, 23.06.2019 03:10
Acomputer has a two-level cache. suppose that 60% of the memory references hit on the first level cache, 35% hit on the second level, and 5% miss. the access times are 5 nsec, 15 nsec, and 60 nsec, respectively, where the times for the level 2 cache and memory start counting at the moment it is known that they are needed (e.g., a level 2 cache access does not even start until the level 1 cache miss occurs). what is the average access time?
Answers: 1
question
Computers and Technology, 23.06.2019 16:30
How to do this programming flowchart?
Answers: 3
You know the right answer?
Add the methods insert and pop to the Array class. These methods should use the strategies discussed...
Questions
question
Mathematics, 01.12.2020 08:20
question
Mathematics, 01.12.2020 08:20
question
French, 01.12.2020 08:20
question
Mathematics, 01.12.2020 08:20
question
English, 01.12.2020 08:20
Questions on the website: 13722361