binary-search

How to use binary search on an existing SortedList with a key function?

How to use binary search on an existing SortedList with a key function? Question: I would like to use binary search on a SortedList with a key function, e.g. similarly to bisect_right() in the bisect module. However, SortedList.bisect_right() only supports searching by value. How to make it work with a key function? Asked By: Eugene …

Total answers: 2

Binary search through string tuple in list

Binary search through string tuple in list Question: I’m trying to do a binary search through a list of tuples, when I give a inputq = "BB", it is not show a correct search. This is my data: alldtata = [(‘BA’, ‘Bosnia and Herzegovina’, 44.0, 18.0), (‘BB’, ‘Barbados’, 13.17, -59.53), (‘BD’, ‘Bangladesh’, 24.0, 90.0), (‘BE’, …

Total answers: 1

How do I call the binary search 10 times?

How do I call the binary search 10 times? Question: #I want to call the binary search 10 times (make the function run 10 times). Im supposed to that and then make a graph out of the result, which I know how to do. I just cant make the function run 10 times to get …

Total answers: 1

Why does my Python binary search algorithm not work for certain items?

Why does my Python binary search algorithm not work for certain items? Question: My binary search algorithm manages to find some items but does not find others: myList = [1,3,4,7,12,13,14,16,19,20,28,29,40,45,48,50,67,89,91,94] item = 67 found = False lowerBound = 0 upperBound = len(myList) – 1 index = 0 while not found and lowerBound != upperBound: index …

Total answers: 2

Find index of less or equal value in list recursively (python)

Find index of less or equal value in list recursively (python) Question: Got task to find indexes of value and double that value in input list. In input first line we get list range, second – list of values, third – value to find. The output is 2 numbers – indexes of equal or higher …

Total answers: 1

Recursive Binary Search Function In Python With Issues

Recursive Binary Search Function In Python With Issues Question: I’m working on a recursive binary search for school. Because of the test code, I’m limited to my input being lyst and target and output should only be a bool asserting whether the target was found or not. I’ve found examples of other questions kind of …

Total answers: 1

why a python function still returns "None" though i am explicitly returning a value?

why a python function still returns "None" though i am explicitly returning a value? Question: def binarySearch(nums,low,high,target): if low<=high: mid=(low+high)//2 if nums[mid]==target: return mid if nums[mid]<target: binarySearch(nums,mid+1,high,target) else: binarySearch(nums,low,mid-1,target) else: return -1 def search(nums, target): return binarySearch(nums,0,len(nums)-1,target) nums=[-1,0,3,5,9,12] target=9 print(search(nums,target)) console output Expected output of the above python code for binary search is ‘4’. But …

Total answers: 1

Finding "crossover" index with modified binary search algorithm in Python

Finding "crossover" index with modified binary search algorithm in Python Question: The task is to find "crossover" index of two arrays. The crossover index is the index, that for array x and y: assert(x[left] > y[left]) assert(x[right] < y[right]) I am supposed to use recursion to solve this problem. Test case to pass are: Test …

Total answers: 2