In: Computer Science
Write an algorithm for a function named finder that recursively goes through a list and returns the index of the first occurrence of an item of interest. The function takes three parameters: 1. The first position in the list (or 0) 2. The list we want to iterate over 3. The item we want to find. If the item is not there in the list and we have iterated over the whole list, then the function should return 0.
def finder(position=0, list, search): # if the position is more than the length of the list. if position >= len(list): return 0 # if current element of list has the search element if list[position] == search: return position # else, item is not found on current position, hence search # further down the list return finder(position + 1, list, search)
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.