In Data Structures and Algorithms in Python, homework question C 5.25 says The s
ID: 3883692 • Letter: I
Question
In Data Structures and Algorithms in Python, homework question C 5.25 says The syntax data.remove(value) for Python list data removes only the first occurrence of element value from the list. Give an implementation of a function called remove_all(data, value) that removes all occurrences of value from the given list, such that the worst-case running time of the function is O(n) on a list with n elements.
The dynamic array class is given in the book:
1 import ctypes # provides low-level arrays 3 class DynamicArray 4 A dynamic array class akin to a simplified Python list.""" 6 def init__(self) te an empty array."n self..-n 0 self-capacity = 1 self-A self-make-array(self-capacity) # count actual elements # default array capacity # low-level array 9 10 12 def len__(self) 13 14 15 16 def --getitem-(self, k) 17 Return number of elements stored in the array."" return self._n "" Return element at index k." if not 0Explanation / Answer
#!usr/bin/python
def remove_all(data,value): # Removing all the occurebnces of the value from the #list
count = 0
for i in range(len(data)):
if data[i] == value:
count = count + 1
for i in range(count):
data.remove(value)
return data
data = [1,2,2,2,2,6,7]
data = remove_all(data,2)
print(data)