A python package for binary search
Project description
jbisect
Reusable implementation of the bisect / binary search algorithm.
This (obviously) competes with the standard library
bisect
package. Whereas bisect
only searches lists this package supports searching on a function, and supports both integer and
floating-point indices.
Install with:
pip install jbisect
Basic searching
jbisect
provides the function bisect_seq
for searching sequences:
from jbisect import bisect_seq
print(bisect_seq("011222355", "2"))
By default the entire sequence is searched, but you can use the parameters low
and high
to limit
the search range:
print(bisect_seq("011222355", "2", low=1, high=5))
You can use the side
parameters to configure whether to return the first match, or just past the
last match:
print(bisect_seq("011222355", "2", side="right"))
If you have a sequence that is descending, instead of ascending, you need to set the ordering
parameter:
print(bisect_seq("553222110", "2", ordering="descending"))
Searching functions:
The functions bisect_int_fn
and bisect_float_fn
can be used to search a function instead of a
sequence. These functions take the same low
, high
, side
and ordering
arguments as
bisect_seq
.
from jbisect import bisect_int_fn, bisect_float_fn
print(bisect_int_fn(lambda i: i * i, 16))
print(bisect_float_fn(lambda i: i * i, 2.0))
Searching predicates:
Finally the functions bisect_int_pred
and bisect_float_pred
can be used to find the first value
accepted by a predicate. pred
must be a function that returns a bool
, and for which there exists
some x
so that for all y<x
pred(y)
is False
; and for all y>=x
pred(y)
is
True
. bisect_*_pred
will then find x
.
from jbisect import bisect_int_pred, bisect_float_pred
print(bisect_int_pred(lambda i: i * i >= 16))
print(bisect_float_pred(lambda i: i * i >= 2.0))
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
Built Distribution
File details
Details for the file jbisect-0.2.0.tar.gz
.
File metadata
- Download URL: jbisect-0.2.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86126e50111555b6aaf3c8a0717ed2bff7f5673cadf37ac24031f1ad2f36622a |
|
MD5 | c3a561b7b7e1d2a0ebf8cdb1ebae3263 |
|
BLAKE2b-256 | 6a7ed946b8f6b4052289230c0987af108ae92cbb9dd4d79ae6be1d4b15f66af8 |
File details
Details for the file jbisect-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: jbisect-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de47c566477e86c403f4bf28e7ae337e66b87f01f95be949ff43f6245f25eb78 |
|
MD5 | 4ba448c818f47dcb98e74031b2593877 |
|
BLAKE2b-256 | ee7dd378ac3ef7567edfcaf86bfc0543f1b9756419b22359f3c33c192d03b43b |