Skip to content

pydsa.combo

Index-combination helpers preloaded by sitecustomize: every pair and every triple of indices below n, in lexicographic order, without recursion; every substring of a sequence, in one of three orders.

Maker module-attribute

Maker = Callable[[Iterable[int]], Any]

pairs

pairs(
    n: int, type: Maker = tuple, back: bool = False
) -> Iterator[Any]

Every (i, j) with 0 <= i < j < n, in lexicographic order.

With back, the pairs are grouped by the later index instead: for each j in turn, every (i, j) with i < j. That is the dp order, where cell j looks back at every earlier cell.

Each pair is built by type: tuple by default, list for a LeetCode answer.

Source code in pydsa/combo.mu
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def pairs(n: int, type: Maker=tuple, back: bool=false) -> Iterator[Any]
  """Every (i, j) with 0 <= i < j < n, in lexicographic order.

  With back, the pairs are grouped by the later index instead: for each
  j in turn, every (i, j) with i < j. That is the dp order, where cell j
  looks back at every earlier cell.

  Each pair is built by type: tuple by default, list for a LeetCode answer.
  """
  if back
    for j in 0..<n
      for i in 0..<j
        yield type((i, j))
  else
    for i in 0..<n
      for j in i + 1..<n
        yield type((i, j))
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def pairs(n: int, type: Maker = tuple, back: bool = False) -> Iterator[Any]:
    """Every (i, j) with 0 <= i < j < n, in lexicographic order.

    With back, the pairs are grouped by the later index instead: for each
    j in turn, every (i, j) with i < j. That is the dp order, where cell j
    looks back at every earlier cell.

    Each pair is built by type: tuple by default, list for a LeetCode answer.
    """
    if back:
        for j in range(0, n):
            for i in range(0, j):
                yield type((i, j))
    else:
        for i in range(0, n):
            for j in range(i + 1, n):
                yield type((i, j))

triples

triples(n: int, type: Maker = tuple) -> Iterator[Any]

Every (i, j, k) with 0 <= i < j < k < n, in lexicographic order.

Each triple is built by type: tuple by default, list for a LeetCode answer.

Source code in pydsa/combo.mu
27
28
29
30
31
32
33
34
35
def triples(n: int, type: Maker=tuple) -> Iterator[Any]
  """Every (i, j, k) with 0 <= i < j < k < n, in lexicographic order.

  Each triple is built by type: tuple by default, list for a LeetCode answer.
  """
  for i in 0..<n
    for j in i + 1..<n
      for k in j + 1..<n
        yield type((i, j, k))
31
32
33
34
35
36
37
38
39
def triples(n: int, type: Maker = tuple) -> Iterator[Any]:
    """Every (i, j, k) with 0 <= i < j < k < n, in lexicographic order.

    Each triple is built by type: tuple by default, list for a LeetCode answer.
    """
    for i in range(0, n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                yield type((i, j, k))

substrings

substrings(s: Sequence, by: str = 'start') -> Iterator

Every slice s[i:j] with 0 <= i < j <= len(s), each position once.

by picks the order: "start" groups the slices by where they start, shortest first; "end" groups them by where they end, longest first; "length" gives every slice of length 1, then of length 2, and so on, left to right. A list gives its subarrays the same way.

Source code in pydsa/combo.mu
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def substrings(s: Sequence, by: str="start") -> Iterator
  """Every slice s[i:j] with 0 <= i < j <= len(s), each position once.

  by picks the order: "start" groups the slices by where they start,
  shortest first; "end" groups them by where they end, longest first;
  "length" gives every slice of length 1, then of length 2, and so on,
  left to right. A list gives its subarrays the same way.
  """
  n = len(s)
  if by == "start"
    for i in 0..<n
      for j in i + 1..n
        yield s[i:j]
  elif by == "end"
    for j in 1..n
      for i in 0..<j
        yield s[i:j]
  elif by == "length"
    for size in 1..n
      for i in 0..n - size
        yield s[i:i + size]
  else
    raise ValueError(by)
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def substrings(s: Sequence, by: str = "start") -> Iterator:
    """Every slice s[i:j] with 0 <= i < j <= len(s), each position once.

    by picks the order: "start" groups the slices by where they start,
    shortest first; "end" groups them by where they end, longest first;
    "length" gives every slice of length 1, then of length 2, and so on,
    left to right. A list gives its subarrays the same way.
    """
    n = len(s)
    if by == "start":
        for i in range(0, n):
            for j in range(i + 1, n + 1):
                yield s[i:j]
    elif by == "end":
        for j in range(1, n + 1):
            for i in range(0, j):
                yield s[i:j]
    elif by == "length":
        for size in range(1, n + 1):
            for i in range(0, n - size + 1):
                yield s[i:i + size]
    else:
        raise ValueError(by)