Binary search - bisect_left and bisect_right with more flexible comparison
Project description
Bsearch
Binary search in python with more flexible comparisons.
Installation
pip install bsearch
Usage
bisect_left
and bisect_right
are backwards compatible with the bisect library. For example:
from bsearch import bisect_left
a = [1,2,3,4,5]
i = bisect_left(a, 4) # returns 3
You can use the key
option to search arrays of different types
from bsearch import bisect_left
a = [(1,100), (2, 100), (5,200)]
# using the first element of the tuple as the key
i = bisect_left(a, 2, key=lambda x: x[0]) # returns 1
To search descending list, use the wrapper binary_search
, and provide an operator. This will return the smallest i
such that op(a[i], v)
from bsearch import binary_search
a = [5, 4, 3, 2]
i = binary_search(a, 4, op=lambda a b: a >= b) # returns 1
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
bsearch-0.0.3.tar.gz
(1.8 kB
view details)
Built Distribution
File details
Details for the file bsearch-0.0.3.tar.gz
.
File metadata
- Download URL: bsearch-0.0.3.tar.gz
- Upload date:
- Size: 1.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8784d01544a36c840e76f7fb9c203502d2dca0a4a015ccae13b2153f5de9fe53 |
|
MD5 | 78ad27581b91254f78ef652f811553ff |
|
BLAKE2b-256 | 431e1cfc7bdb422df232753d4acc081353188aea7424db4159bc797dfc69e67a |
File details
Details for the file bsearch-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: bsearch-0.0.3-py3-none-any.whl
- Upload date:
- Size: 2.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4da51f9f0e8b4e8943ceb4fcb31643d986f53f9d477c12b8a568dd2fa88427a7 |
|
MD5 | d0bdd1253063ff2c032cb913d2e17e45 |
|
BLAKE2b-256 | 7f08b833ccea1aa7c282aaf10bc62f5a9e4ab4c386598af27f8d0562bac48f88 |