229k views
2 votes
How many times is the recursive function find() called when searching for the missing letter 'A' in the below code?def find(lst, item, low, high):range_size = (high - low) + 1mid = (high + low) // 2if item == lst[mid]:pos = midelif range_size == 1:pos = -1else:if item < lst[mid]:pos = find(lst, item, low, mid)else:pos = find(lst, item, mid+1, high)return poslistOfLetters = ['B', 'C', 'D', 'E', 'F', 'G', 'H']print(find(listOfLetters, 'A', 0, 6))

User Waldelb
by
8.4k points

1 Answer

5 votes

Answer:

The recursive function find() will be called three times to search for the missing letter 'A' in the given code.

Step-by-step explanation:

The first call to find(lst, item, low, high) will be with low = 0 and high = 6, which is the size of listOfLetters. In this call, mid will be set to (6 + 0) // 2 = 3, and since 'A' < 'F', the second recursive call will be made with low = 0 and high = 2.

In the second call, mid will be set to (2 + 0) // 2 = 1, and since 'A' < 'C', the third and final recursive call will be made with low = 0 and high = 0. At this point, range_size will be equal to 1, so pos will be set to -1 and returned to the previous call.

User Ratm
by
8.1k points