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 16:10
When copying and pasting text, the first step is move your cursor type the text select the copy command select the paste command
Answers: 2
question
Computers and Technology, 22.06.2019 23:00
Is an attack that relies on guessing the isns of tcp packets
Answers: 2
question
Computers and Technology, 23.06.2019 11:20
Http is the protocol that governs communications between web servers and web clients (i.e. browsers). part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. some of the codes and their meanings are listed below: 200, ok (fulfilled)403, forbidden404, not found500, server errorgiven an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
Answers: 2
question
Computers and Technology, 23.06.2019 14:30
Select the correct answer. peter has launched a website that features baby products. however, clients often find they are unable to access the website because the server is down. which feature of cybersecurity should peter focus on for his website? a. data authenticity b. data privacy c. data availability d. data integrity e. data encryption
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
Advanced Placement (AP), 23.02.2021 18:40
question
Social Studies, 23.02.2021 18:40
question
English, 23.02.2021 18:40
Questions on the website: 13722367