Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 19971

How to do a binary search for a range of the same value?

$
0
0

I have a sorted list of numbers and I need to get it return the range of index that the number appears. My list is:

daysSick = [0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 11, 15, 24]

If I searched 0, I need to return (0, 3). Right now I can only get it to find the spot of one number! I know how to do the binary search, but I am stuck how to make it move up and down from the position to find the other same values!

low = 0high = len(daysSick) - 1while low <= high :    mid = (low + high) // 2    if value < daysSick[mid]:        high = mid - 1    elif value > list[mid]:        low = mid + 1    else:        return mid

Viewing all articles
Browse latest Browse all 19971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>