Skip to main content

Binary search - bisect_left and bisect_right with more flexible comparison

Project description

Bsearch

Build Status PyPI shield

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.4.tar.gz (1.8 kB view details)

Uploaded Source

Built Distribution

bsearch-0.0.4-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file bsearch-0.0.4.tar.gz.

File metadata

  • Download URL: bsearch-0.0.4.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

Hashes for bsearch-0.0.4.tar.gz
Algorithm Hash digest
SHA256 335c7939079f124d9ba65ec282a71893fe494e87148448123174e31568ac693e
MD5 d5a360cba999ab73dcd19e832e898b7c
BLAKE2b-256 8127c1969f2c3ef3a884390b7f9a8ba7e954f3d35ad10f625b077915250e1fce

See more details on using hashes here.

File details

Details for the file bsearch-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: bsearch-0.0.4-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

Hashes for bsearch-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b81c25b5b547c97278f522bf09e4f0b4f5f55d1701335f2f6241d1d742ced19c
MD5 e4b8464567daff5d3977230c141781b3
BLAKE2b-256 e983dc034467160b6231a045dc5c79d324cdf6fc0771500ccd5e65553f13b7bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page