Binary Search in Python
Binary search on already sorted sequences (list, tuple) to get the first element >= or >
a value (similar to C++'s std::lower_bound and std::upper_bound).
Requires python >=3.5 for the typing module and >= 3.4 for the enum module, but you can easily
extract the search function from binary_search/__init__.py if you need it for a lower version.
Install
pip install binary-search
Usage
See tests.py for more sample usage.
import binary_search as bs
sorted_sequence = (2, 5, 7, 9)
a = bs.search(sorted_sequence, 5)
print(a) # 1 - the index of the first element >= 5
b = bs.search(sorted_sequence, 6)
print(b) # 2 - the index of the first element >= 6 (element 2 with value 7)
You can also use a custom key in the same way as sorted
import binary_search as bs
import random
# sequence of 10 random pairs of integers
sequence = [(random.randint(), random.randint()) for _ in range(10)]
# sort and search just using the first integer
key = lambda pair: pair[0]
sorted_sequence = sorted(sequence, key=key)
bs.search(sorted_sequence, [5, None], key=key)
Development and testing
- Fork the repository
- Clone your fork
- Install it in editable mode
pip install -e . - Make any changes and add new tests to
tests.py - Run pytest
python -m pytest - Commit and push to your fork
- Make pull request (merge your fork back to main repo)
Metadata
Release files for binary-search 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| binary_search-0.3.0.tar.gz | 2.7 kB | Without provenance | Details |
Release files / binary_search-0.3.0.tar.gz
| Download URL | binary_search-0.3.0.tar.gz |
|---|---|
| Size | 2.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ed41a3e49a4b0fbb21d8c32c8943f3d220b0eb01db49f89caf5d80efbdd6b298
|
|
BLAKE2b-256 checksum How to use checksums |
6ae8120de6ce10e8912d4623a5750089a5c17d44f9b3dd60fb89ab5b2e0775a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5
|
Security for binary-search 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.