Question
Define the remove_duplicates(list_of_values, value) function which takes two parameters: a list and some implementation of the List ADT, such as the MyList class developed in
Define the remove_duplicates(list_of_values, value) function which takes two parameters: a list and some implementation of the List ADT, such as the MyList class developed in the previous question (but it could be some other implementation) and a value.
The function removes all the duplicate copies of the value parameter from the list, leaving just one unique instance of that value in the list. In defining the function, you must only use the List ADT methods:
is_empty() size() add(item) search(item) remove(item)
The function returns the number of items which were removed from the list.
Note: if the item does not exist in the list, then the function should return None.
The function header is:
def remove_duplicates(list_of_values, value):
Note: you do not need to submit the MyList class, or any other List ADT implementation (you can assume these are available).
class MyList: def __init__(self): self.alist = [] def is_empty(self): return len(self.alist) == 0 def size(self): return len(self.alist) def add(self, item): return self.alist.append(item) def search(self, item): if item in self.alist: return True return False def remove(self, item): return self.alist.remove(item)
Test | Result |
---|---|
values = MyList() values.add('eggs') values.add('butter') values.add('eggs') values.add('milk') values.add('eggs') x = remove_duplicates(values, 'eggs') print(x) | 2 |
values = MyList() values.add('butter') values.add('milk') x = remove_duplicates(values, 'eggs') print(x) | None |
values = MyList() values.add('eggs') values.add('milk') x = remove_duplicates(values, 'eggs') print(x) | 0 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started