Question: The remove (value) method of the list class, removes the first occurrence of value from the list it was called on, or raises a
The remove (value) method of the list class, removes the first occurrence of value from the list it was called on, or raises a ValueError exception, if value is not present. Note: Since remove needs to shift elements, its worst-case running time is linear. In this question we will look into the function remove_all (1st, value), that removes all occurrences of value from 1st. a) Consider the following implementation of remove_all: def remove all (1st, value) : end = False while (end == False): try: 1st.remove (value) except ValueError: end = True Analyze the worst-case running time of the implementation above. b) Give an implementation to remove all that runs in worst-case linear time. Notes: 1. Your implementation should mutate the given list object (in-place), without using an additional data structure. 2. Your implementation should keep the relative order of the elements that remain in the list c) Analyze the worst-case running time of your implementation in (b).
Step by Step Solution
3.44 Rating (157 Votes )
There are 3 Steps involved in it
a The given implementation of removeall has a while loop that continues until the end variable becom... View full answer
Get step-by-step solutions from verified subject matter experts
