Functions for finding numbers using higher/lower
APACHE-2.0 License
Functions for finding numbers using higher/lower
Install this library using pip
:
$ pip install higher-lower
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 valuemax_value
- the highest possible valuecallback(candidate)
- a callback function that takes a single integer argument and returns ActualIs.MATCH
, ActualIs.HIGHER
or ActualIs.LOWER
For example:
from higher_lower import ActualIs
def 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:
from higher_lower import higher_lower
number = higher_lower(0, 100, callback)
# number is now 7
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd higher-lower
python -mvenv venv
source venv/bin/activate
Or if you are using pipenv
:
pipenv shell
Now install the dependencies and tests:
pip install -e '.[test]'
To run the tests:
pytest