all_monotone¶
- iteration_utilities.all_monotone(iterable, decreasing=False, strict=False)¶
Checks if the elements in iterable are (strictly) monotonic increasing or decreasing.
- Parameters:
- iterableiterable
Any iterable to test.
- decreasing
bool, optional If
Falsecheck if the values are monotonic increasing, otherwise check for monotone decreasing. Default isFalse.- strict
bool, optional If
Truecheck if the elements are strictly greater or smaller (>or<) than their predecessor. Otherwise use>=and<=.
- Returns:
- monotonic
bool Trueif all elements in iterable are monotonic orFalseif not.
- monotonic
Notes
If the input is empty the function returns
True.Examples
This is roughly equivalent to
all(itertools.starmap(operator.lt, iteration_utilities.successive(iterable, 2)))with the appropriate operator depending on decreasing and strict:>>> from iteration_utilities import all_monotone >>> all_monotone([1,1,1,1,1,1,1,1,1]) True >>> all_monotone([1,1,1,1,1,1,1,1,1], strict=True) False >>> all_monotone([2,1,1,1,1,1,1,1,0], decreasing=True) True >>> all_monotone([2,1,1,1,1,1,1,1,0], decreasing=True, strict=True) False