1import itertools
2from collections.abc import Iterable, Iterator
3from typing import TypeVar
4
5T = TypeVar("T")
6
7
8def pairwise(iterable: Iterable[T]) -> Iterator[tuple[T, T]]:
9 # Copied from Python 3.10
10 # pairwise('ABCDEFG') --> AB BC CD DE EF FG
11 a, b = itertools.tee(iterable)
12 next(b, None)
13 return zip(a, b)
14
15
16def get_intervals(values: list[int]) -> list[int]:
17 """Get all the intervals between numbers.
18
19 It's similar to numpy.diff function.
20
21 Example:
22 -------
23 >>> get_intervals([1, 4, 5, 6, 10])
24 [3, 1, 1, 4]
25
26 """
27 all_diffs = []
28 for value, next_value in pairwise(values):
29 all_diffs.append(next_value - value)
30 return all_diffs