Implement a universal pop k method in dynamicarray


Problem

Implement a universal pop(k) method in DynamicArray.py that removes the last element by default (i.e. k = -1) if the user calls pop() with no input, or so that the element at index k is removed if the user calls pop(k). The effect of pop(k) is similar to remove(e) except that the latter removes a specific element, whereas pop(k) removes the element at index k. This means you have to shift elements to the left when an element is removed in a dynamic array. Furthermore, you have to consider if the user inputs the index as a negative integer. This is easily fixed using modulo arithmetic. For example, if a dynamic array has length 5, the positive indices are 0, 1, 2, 3, 4. If a user makes the call pop(-2) you can transform the index -2%5 = 3 and remove the element at index 3. Test your implementation inside the if name == " main ":"

DynamicArray code:
import ctypes

class DynamicArray(object):
def __init__(self):
self._n = 0
self._capacity = 1
self._A = self._make_array(self._capacity)

def _make_array(self, c):
return (c * ctypes.py_object)() # instance of capacity c of py_object type, which is a low-level array

def __len__(self):
return self._n

def __getitem__(self, index):
if not 0 <= index < self._n:
raise IndexError("Index out of bounds.")
return self._A[index]

def __setitem__(self, index, value):
if not 0 <= index < self._n:
raise IndexError("Index out of bounds.")
self._A[index] = value

def _resize(self, c):
B = self._make_array(c)
for k in range(self._n):
B[k] = self._A[k]
self._A = B
self._capacity = c

def append(self, obj):
if self._n == self._capacity:
self._resize(2*self._capacity)
self._A[self._n] = obj
self._n += 1

def pop(self, k):
pass

Request for Solution File

Ask an Expert for Answer!!
Python Programming: Implement a universal pop k method in dynamicarray
Reference No:- TGS03268201

Expected delivery within 24 Hours