def binary_search(lst, to_find): def binary_search_rec(bot, top): if bot > top:
ID: 3741099 • Letter: D
Question
def binary_search(lst, to_find):
def binary_search_rec(bot, top):
if bot > top:
return None
mid = (bot + top) // 2
if to_find == lst[mid]:
return lst[mid]
elif to_find < lst[mid]:
return binary_search_rec(bot, mid-1)
else:
return binary_search_rec(mid+1, top)
return binary_search_rec(0, len(lst)-1)
Given the call binary_search([2, 5, 7, 9, 13, 22], 22), which values (in order) in lst are compared to to_find before returning from the call?
5 9 13 22 (b) 9 22 (c) 7 13 22 (d) 7 9 22
Which of the following correctly “removes” the element at position idx from an arraybacked list?
1. for i in range(len(self.data)-1, idx, -1):
self.data[i-1] = self.data[i]
2. for i in range(idx):
self.data[i] = self.data[i+1]
3. for i in range(0, idx-1, 1):
self.data[i+1] = self.data[i]
4. for i in range(idx, len(self.data)-1):
self.data[i] = self.data[i+1]
Explanation / Answer
1. Given the call binary_search([2, 5, 7, 9, 13, 22], 22), which values (in order) in lst are compared to to_find before returning from the call are: 7,13,22
Answer:c
Because, the iteration will runn in folowwing steps before giving output:
2. Which of the following correctly “removes” the element at position idx from an arraybacked list:
Answer:
4. for i in range(idx, len(self.data)-1):
self.data[i] = self.data[i+1]
Because, above code left shift each valuesin list starting from idx.
Consider the list: [2, 5, 7, 9, 13, 22] and idx=3(i.e. to delete element 9)
given code runs as follows:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.