Skip to content

pydsa.digits

Digit helpers preloaded by sitecustomize: a number as its list of digits and back, its count of digits, and the parity tests even and odd.

to_digits

to_digits(
    num: int | str, reverse: bool = False, base: int = 10
) -> list[int]

The digits of num in the given base as ints, most significant first.

With reverse=True the least significant digit comes first, so digit i is the coefficient of base**i. A negative int loses its sign. A str is read one character at a time, so leading zeros are kept; base=2 gives the bits.

Source code in pydsa/digits.mu
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def to_digits(num: int | str, reverse: bool=false, base: int=10) -> [int]
  """The digits of num in the given base as ints, most significant first.

  With reverse=True the least significant digit comes first, so digit i
  is the coefficient of base**i. A negative int loses its sign. A str is
  read one character at a time, so leading zeros are kept; base=2 gives
  the bits.
  """
  if isinstance(num, str)
    ds = [int(c, base) for c in num.lstrip("-")]
    return reverse ? ds[::-1] : ds
  num, ds = abs num, []
  while num or not ds
    num, d = divmod(num, base)
    ds <- d
  reverse ? ds : ds[::-1]
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def to_digits(num: int | str, reverse: bool = False, base: int = 10) -> list[int]:
    """The digits of num in the given base as ints, most significant first.

    With reverse=True the least significant digit comes first, so digit i
    is the coefficient of base**i. A negative int loses its sign. A str is
    read one character at a time, so leading zeros are kept; base=2 gives
    the bits.
    """
    if isinstance(num, str):
        ds = [int(c, base) for c in num.lstrip("-")]
        return ds[::-1] if reverse else ds
    num, ds = abs(num), []
    while num or not ds:
        num, d = divmod(num, base)
        ds.append(d)
    return ds if reverse else ds[::-1]

num_digits

num_digits(num: int, base: int = 10) -> int

The count of digits of num in the given base; 0 has one digit.

A negative int loses its sign. It does not convert num to a str, so it is fast on big ints and has no int max str digits limit.

Source code in pydsa/digits.mu
23
24
25
26
27
28
29
30
31
32
33
34
def num_digits(num: int, base: int=10) -> int
  """The count of digits of num in the given base; 0 has one digit.

  A negative int loses its sign. It does not convert num to a str, so it
  is fast on big ints and has no int max str digits limit.
  """
  num = abs num
  ret d = max(1, int(num.bit_length() / log2(base)))
  while d > 1 and base ** (d - 1) > num
    d -= 1
  while base ** d <= num
    d += 1
26
27
28
29
30
31
32
33
34
35
36
37
38
def num_digits(num: int, base: int = 10) -> int:
    """The count of digits of num in the given base; 0 has one digit.

    A negative int loses its sign. It does not convert num to a str, so it
    is fast on big ints and has no int max str digits limit.
    """
    num = abs(num)
    d = max(1, int(num.bit_length() / log2(base)))
    while d > 1 and base ** (d - 1) > num:
        d -= 1
    while base ** d <= num:
        d += 1
    return d

to_int

to_int(
    digits: list[int], reverse: bool = False, base: int = 10
) -> int

The int whose digits in the given base are the list, most significant first.

With reverse=True the list is read least significant first, the inverse of to_digits(num, reverse=True). An empty list is 0. base=2 reads bits.

Source code in pydsa/digits.mu
36
37
38
39
40
41
42
43
44
def to_int(digits: [int], reverse: bool=false, base: int=10) -> int
  """The int whose digits in the given base are the list, most significant first.

  With reverse=True the list is read least significant first, the inverse
  of to_digits(num, reverse=True). An empty list is 0. base=2 reads bits.
  """
  ret out = 0
  for d in reverse ? digits[::-1] : digits
    out = out * base + d
41
42
43
44
45
46
47
48
49
50
def to_int(digits: list[int], reverse: bool = False, base: int = 10) -> int:
    """The int whose digits in the given base are the list, most significant first.

    With reverse=True the list is read least significant first, the inverse
    of to_digits(num, reverse=True). An empty list is 0. base=2 reads bits.
    """
    out = 0
    for d in digits[::-1] if reverse else digits:
        out = out * base + d
    return out

even

even(n: int) -> bool
Source code in pydsa/digits.mu
46
def even(n: int) -> bool: n % 2 == 0
53
54
def even(n: int) -> bool:
    return n % 2 == 0

odd

odd(n: int) -> bool
Source code in pydsa/digits.mu
47
def odd(n: int) -> bool: n % 2 == 1
57
58
def odd(n: int) -> bool:
    return n % 2 == 1