Skip to content

pydsa.arith

Integer arithmetic helpers.

ceil_div

ceil_div(a: int, b: int) -> int

a / b rounded up, for a >= 0 and b > 0.

Source code in pydsa/arith.py
 7
 8
 9
10
11
12
13
def ceil_div(a: int, b: int) -> int:
    """a / b rounded up, for a >= 0 and b > 0."""
    if b <= 0:
        raise ValueError("Denominator b must be positive.")
    if a < 0:
        raise ValueError("Numerator a must be non-negative.")
    return (a + b - 1) // b

batched

batched(s: Sequence[Any], n: int = 1) -> List[Any]

s cut into pieces of n; the last piece holds what is left.

Source code in pydsa/arith.py
16
17
18
19
def batched(s: Sequence[Any], n: int = 1) -> List[Any]:
    """s cut into pieces of n; the last piece holds what is left."""
    r = list(range(0, len(s), n))
    return [s[a:b] for a, b in zip_longest(r, r[1:])]