Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonw/higher-lower
Functions for finding numbers using higher/lower
https://github.com/simonw/higher-lower
Last synced: 27 days ago
JSON representation
Functions for finding numbers using higher/lower
- Host: GitHub
- URL: https://github.com/simonw/higher-lower
- Owner: simonw
- License: apache-2.0
- Created: 2021-02-16T07:09:21.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-02-16T07:23:39.000Z (over 3 years ago)
- Last Synced: 2024-10-11T09:19:31.092Z (about 1 month ago)
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# higher-lower
[![PyPI](https://img.shields.io/pypi/v/higher-lower.svg)](https://pypi.org/project/higher-lower/)
[![Changelog](https://img.shields.io/github/v/release/simonw/higher-lower?label=changelog)](https://github.com/simonw/higher-lower/releases)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/higher-lower/blob/main/LICENSE)Functions for finding numbers using higher/lower
## Installation
Install this library using `pip`:
$ pip install higher-lower
## higher_lower(min_value, max_value, callback)
The `higher_lower()` function searches for a value between `min_value` and `max_value`, calling `callback()` for each candidate value to see if the target is higher or lower.
- `min_value` - the lowest possible value
- `max_value` - the highest possible value
- `callback(candidate)` - a callback function that takes a single integer argument and returns `ActualIs.MATCH`, `ActualIs.HIGHER` or `ActualIs.LOWER`For example:
```python
from higher_lower import ActualIsdef callback(candidate):
if candidate == 7:
return ActualIs.MATCH
elif candidate > 7:
return ActualIs.LOWER
else:
return ActualIs.HIGHER
```
Given the above callback function, a search can be made for the number between 0 and 100 like so:
```python
from higher_lower import higher_lowernumber = higher_lower(0, 100, callback)
# number is now 7
```## Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd higher-lower
python -mvenv venv
source venv/bin/activateOr if you are using `pipenv`:
pipenv shell
Now install the dependencies and tests:
pip install -e '.[test]'
To run the tests:
pytest