Master Computers and Technology with Fun Quizzes & Brain Teasers!

This method returns a new Dynamic Array object that contains the requested number of elements from the original array starting with the element located at the requested start index. If the provided start index is invalid, or if there are not enough elements between start index and end of the array to make the slice of requested size, this method raises a custom "DynamicArrayException". Code for the exception is provided in the starter code below.#Starter Codeclass DynamicArrayException(Exception): """ Custom exception class to be used by Dynamic Array DO NOT CHANGE THIS METHOD IN ANY WAY """ passclass DynamicArray: def __init__(self, start_array=None): """ Initialize new dynamic array DO NOT CHANGE THIS METHOD IN ANY WAY """ self.size = 0 self.capacity = 4 self.data = [None] * self.capacity # populate dynamic array with initial values (if provided) # before using this feature, implement append() method if start_array is not None: for value in start_array: self.append(value) def __str__(self) -> str: """ Return content of dynamic array in human-readable form DO NOT CHANGE THIS METHOD IN ANY WAY """ out = "DYN_ARR Size/Cap: " out += str(self.size) + "/"+ str(self.capacity) out += " " + str(self.data[:self.size]) return out#Here is my append methoddef append(self, value: object) -> None: """ Adds a new value at the end of the dynamic array. """ if self.size self.data[self.size] = value self.size = self.size + 1 else: temp = [None] * self.capacity tsize=self.capacity for i in range(tsize): temp[i] = self.data[i] self.capacity *= 2 self.size = 0 self.data = [None] * self.capacity for i in range(tsize): self.append(temp[i]) self.append(value) self.size = 0 self.data = [None] * self.capacity for i in range(tsize): self.append(temp[i]) self.append(value) return#Please help me implement the slice methoddef slice(self, start_index: int, quantity: int) -> object: """ TODO: Write this implementation """ return DynamicArray()#Testing:Example #1:da = DynamicArray([1, 2, 3, 4, 5, 6, 7, 8, 9])da_slice = da.slice(1, 3)print(da, da_slice, sep="\n")da_slice.remove_at_index(0)print(da, da_slice, sep="\n")Output:DYN_ARR Size/Cap: 9/16 [1, 2, 3, 4, 5, 6, 7, 8, 9]DYN_ARR Size/Cap: 3/4 [2, 3, 4]DYN_ARR Size/Cap: 9/16 [1, 2, 3, 4, 5, 6, 7, 8, 9]DYN_ARR Size/Cap: 2/4 [3, 4]Example #2:da = DynamicArray([10, 11, 12, 13, 14, 15, 16])print("SOUCE:", da)slices = [(0, 7), (-1, 7), (0, 8), (2, 3), (5, 0), (5, 3)]for i, cnt in slices:print("Slice", i, "/", cnt, end="")try:print(" --- OK: ", da.slice(i, cnt))except:print(" --- exception occurred.")Output:SOUCE: DYN_ARR Size/Cap: 7/8 [10, 11, 12, 13, 14, 15, 16]Slice 0 / 7 --- OK: DYN_ARR Size/Cap: 7/8 [10, 11, 12, 13, 14, 15, 16]Slice -1 / 7 --- exception occurred.Slice 0 / 8 --- exception occurred.Slice 2 / 3 --- OK: DYN_ARR Size/Cap: 3/4 [12, 13, 14]Slice 5 / 0 --- OK: DYN_ARR Size/Cap: 0/4 []Slice 5 / 3 --- exception occurred.