Question: Python Apply the binary search algorithm for the following test case: Input: { ' cards ' : [ 8 , 8 , 6 , 6
Python
Apply the binary search algorithm for the following test case:
Input:
cards: 'query':
Expected output:
Hint:
We did locate a in the array; it's just that it wasn't the first As you can guess, this is because
we don't go over indices in a linear order in binary search.
How do we fix it
When we find that cardsmid is equal to query, we need to check whether it is the first
occurrence of query in the list, ie the number that comes before it
To simplify it we'll define a helper function called testlocation, which will take the list cards,
the query, and the mid as inputs.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
