more_bisect
A binary search extension of Python bisect module that enables flexible comparisons.
Includes these functions:
first_pos_eq: find the first index of value equal toxlast_pos_eq: find the last index of value equal toxlast_pos_lt: find the last index of value less thanxlast_pos_le: find the last index of value less than or equal toxfirst_pos_gt: find the first index of value greater thanxfirst_pos_ge: find the first index of value greater than or equal toxbisect_left: the same asbisect.bisect_leftbisect_right: the same asbisect.bisect_rightlast_closest_to: find the last index of value closest toxfirst_closest_to: find the first index of value closest tox
All functions are of the same signature:
(x, a=None, lo=None, hi=None, key=None)
x: the value based on which to searcha: the array to search for; if providedNone,keywill be used to form an array (see below)lo,hi: bound the range (left inclusive, right exclusive) of array to searchkey: ifais notNone,a[i]will be mapped tokey(a[i])on each indexi; ifaisNone,a[i]will be mapped tokey(i)on each indexi. This way, whenaisNone, not all elements of the fake array need to be given before binary search
Installation
pip install more_bisect
View the package at PyPI.
Examples
- Invoke
first_pos_eqwith an array:
import more_bisect
more_bisect.first_pos_eq(3, [2, 3, 3, 4, 4, 4])
- Invoke
last_closest_towith a function (the code snippet can be a solution to LeetCode 887 Super Egg Drop:
import more_bisect
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
memo = {}
def recur(k, n):
if (k, n) not in memo:
if n == 0:
ans = 0
elif k == 1:
ans = n
else:
i = more_bisect.last_closest_to(
0, lo=1, hi=n + 1,
key=lambda x: recur(k - 1, x - 1) - recur(k, n - x))
ans = 1 + max(recur(k - 1, i - 1), recur(k, n - i))
memo[k, n] = ans
return memo[k, n]
return recur(k, n)
Metadata
Release files for more-bisect 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| more_bisect-0.1.1.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| more_bisect-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.1 kB
Release files / more_bisect-0.1.1.tar.gz
| Download URL | more_bisect-0.1.1.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19e71650f549d1f50f9796559e8006433ef7463e3e48b448c1caec90ac24d9db
|
|
BLAKE2b-256 checksum How to use checksums |
a8470f7211707929667a235aeef04931800a5d45080b06bbd86b6e236d86a8be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
|
Release files / more_bisect-0.1.1-py3-none-any.whl
| Download URL | more_bisect-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
91a8364f6c88544504d437ae1e0a7829deb15b004b70b29c1f6711497a56a233
|
|
BLAKE2b-256 checksum How to use checksums |
916f2fb7b4fdaf074bbbfab194d4d1cb18762456aa7b3119432eb9fea0cf0bf6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
|