In: Computer Science
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns the count of “valid” (i.e. non-removed) data elements left. This function should do the removal “by hand” and SHOULD NOT use the remove method. Hint: Consider what how you update your current position in an array when you don’t remove an element versus when you do remove an element. Python
def remove_all(l, n, val):
while(1):
flag = 0
for i in range(0,len(l)):
if(l[i] ==
val):
flag = 1
j = i
while(j < len(l) - 1):
l[j] = l[j+1]
j = j + 1
if(flag == 0):
break
else:
l.pop()
print(l)
l = [1,4,5,3,6,4,2,4]
remove_all(l,8,4)
If you have any doubts please comment and please don't dislike.