binary-search

Why does binary search algorithm not work?

Why does binary search algorithm not work? Question: I’ve copied code from the book "grokking algorithms" for finding an item in the list using the binary search algorithm. The code launches great and returns no errors. However, sometimes it just doesn’t work for certain numbers, for example if i call it to find "1". What’s …

Total answers: 2

Recursive binary search python

Recursive binary search python Question: I’ve been trying to write binary search recursively. When I do this using the list[:] syntax I don’t get the intended results with several errors coming up or not getting the correct values. def binary_search(arr, val): left = 0 right = len(arr)-1 mid = (left + right)//2 #Go through the …

Total answers: 3

bisect_left on first item of list within list, Python 3

bisect_left on first item of list within list, Python 3 Question: I have a list like this for example: L = [(0, “test”, “value”), (0, “test2”, “value2”)] and I need to = bisect_left the first item of each tuple to find an index in the list. However, I can’t think of a way of doing …

Total answers: 3

What are python's equivalents of std::lower_bound and std::upper_bound C++ algorithms?

What are python's equivalents of std::lower_bound and std::upper_bound C++ algorithms? Question: Does python provide functions for performing binary search on sorted lists, analogous to the std::lower_bound and std::upper_bound algorithms of the C++ Standard Library? Asked By: Leon || Source Answers: Those functions are located in the bisect module: bisect.bisect_left(a, x, lo=0, hi=len(a)) is the analog …

Total answers: 2

What is the performance impact of non-unique indexes in pandas?

What is the performance impact of non-unique indexes in pandas? Question: From the pandas documentation, I’ve gathered that unique-valued indices make certain operations efficient, and that non-unique indices are occasionally tolerated. From the outside, it doesn’t look like non-unique indices are taken advantage of in any way. For example, the following ix query is slow …

Total answers: 2

Find the smallest number that is greater than a given number in a sorted list

Find the smallest number that is greater than a given number in a sorted list Question: Given a sorted list of numbers, I need to find the smallest number that is greater than a given number. Consider this list: arr=[1,2,3,5,7,11,101,131,151,181,191,313,353,373,383] Say the specified number is 320. Then, my method should return 353 as 353 is …

Total answers: 8

In Python, find item in list of dicts, using bisect

In Python, find item in list of dicts, using bisect Question: I have a list of dicts, something like this: test_data = [ { ‘offset’:0, ‘data’:1500 }, { ‘offset’:1270, ‘data’:120 }, { ‘offset’:2117, ‘data’:30 }, { ‘offset’:4055, ‘data’:30000 }, ] The dict items are sorted in the list according to the ‘offset’ data. The real …

Total answers: 7

Binary search (bisection) in Python

Binary search (bisection) in Python Question: Is there a library function that performs binary search on a list/tuple and return the position of the item if found and ‘False’ (-1, None, etc.) if not? I found the functions bisect_left/right in the bisect module, but they still return a position even if the item is not …

Total answers: 23