52.6k views
24 votes
Suppose you have a sentineled, doubly-linked list class as specified in project 2 and an object of that class called my_list. The function below finds and returns the first index of a specified value in that list, but portions have been removed. Using the minimum possible spacing at all in your answers, fill in the missing code. This algorithm does not employ the __iter__ and __next__ methods of the Linked_List class. Also note that this exercise does not include the object names, so your answers should include my_list. as appropriate. Finally, none of your responses should contain colons. As a reminder, the linked list implementation provides the following methods: insert_element_at(value, index) #cannot be used to append

append_element(value)
get_element_at(index)
remove_element_at(index)
__len__ #support for the len method to obtain the list's size
def index_of(my_list, val):
for k in :
if :
raise ValueError
and what is the performance?

User Folashade
by
4.9k points

1 Answer

12 votes

Answer:

def index_of (my_list, val):

for k in range(my_list.__len__):

if my_list.get_element_at(k) == val :

return k

Step-by-step explanation:

The python function "index_of" accepts two arguments, a list and the value to be searched for.

The for-loop iterates over the list using the "__len__" magic method to return the index of the searched term in the list if present.

User Paul Johnston
by
5.2k points