+ 1
Comrades! Need help to find index in binary search
Binary search function is working fine, but I can't figure out how to get index of num from initial list. P.S. I know standart way such as: list.index(num) No need for such answers. https://code.sololearn.com/cvzzMThO29Pi/?ref=app
3 Answers
+ 1
I corrected the code. Most important change is the redefinition of index variable, which now is for index of first element of partial list. That way you can just add indices (line 8)
Also note the '+ 1' in line 12
https://code.sololearn.com/cigwimQtsTi7/?ref=app
+ 3
Can you give a bit more detail with a sample to illustrate "...how to get index of num from initial list..." Thanks!
BTW you don't need to import random, as it is not used in this code.
+ 1
actually, the idea is to get initial middle index of initial list.
then if we go to left logically we decrease the index size of initial list for half of half, and vise versa if we go to right we increase index for half of half,
I know the idea, but cant implement it in code