Question
def insert(self, i, element): ------------------------------------------------------- A copy of element is inserted at index i, following data items are shifted to the right. If i
def insert(self, i, element): """ ------------------------------------------------------- A copy of element is inserted at index i, following data items are shifted to the right. If i is outside of range of -len(List) to len(List) - 1, the method halts by calling assertion. Note: Python allows negative indices for Python list and we want to support it for our List implementation
Use: lst.insert(i, element) ------------------------------------------------------- Parameters: i - index value (int) element - a data element (?) Returns: True if insert is successful, False otherwise. ------------------------------------------------------- """ assert self._is_valid_index(i), "invalid indices"
assert self._count< self._capacity, "List is full"
#TODO: make it work for negative index #TODO: if array is full
#insert if there is room in place j = 0 if i == self._count: #insertion at end of array self._values[i] = deepcopy(element) else: #general insertion for j in range(self._count, i-1, -1): #shift element to make space for new element self._values[j] = self._values[j-1] self._values[i] = deepcopy(element) self._count += 1 #increment number of elements return
Step 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