pydsa.seq¶
lcs ¶
lcs(
a: Sequence[Any],
b: Sequence[Any],
full: bool = False,
type=int,
)
Longest common subsequence of a and b.
Row i and column j of the table stand for the prefixes a[:i] and b[:j],
so row 0 and column 0 are the empty prefix. With type=int (the default)
each cell holds the length; with type=str, list or tuple it holds one
LCS itself, built as that type. Plain lcs(a, b) is the last cell,
dp[-1][-1]; full returns the whole table.
Source code in pydsa/seq.mu
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
max_diff ¶
max_diff(
xs: Iterable[Any],
key: Optional[Callable[[Any], Any]] = None,
reverse: bool = False,
from_right: bool = False,
) -> list[Any]
The largest key(xs[j]) - key(xs[i]) with i < j, so far, for each j from 1: one value per index after the first.
key(x) is the value compared; with key left out it is x itself. reverse flips the difference to key(xs[i]) - key(xs[j]): the largest fall, not the largest rise.
from_right scans from the right end: the value at i is the best difference over all pairs i' < j with i' >= i, one value per index before the last, in index order.
max_diff([12, 6, 1, 2, 7]) [-6, -5, 1, 6] max_diff([12, 6, 1, 2, 7], reverse=True) [6, 11, 11, 11] max_diff([12, 6, 1, 2, 7], from_right=True) [6, 6, 6, 5] max_diff([12, 6, 1, 2, 7], reverse=True, from_right=True) [11, 5, -1, -5] max([0, *max_diff([7, 6, 4, 3, 1])]) 0
Source code in pydsa/seq.mu
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
subsequence_indices ¶
subsequence_indices(
s: Sequence[Any],
sub: Sequence[Any],
from_right: bool = False,
partial: bool = False,
) -> list[int]
The earliest indices of s at which sub is a subsequence of s: one index per item of sub, in increasing order. [] when sub is not a subsequence of s.
from_right matches from the right end: the latest indices, still in increasing order.
partial returns the indices of the items that match before the first item with no match: a prefix of sub, or a suffix of sub with from_right.
subsequence_indices("abcabc", "ac") [0, 2] subsequence_indices("abcabc", "ac", from_right=True) [3, 5] subsequence_indices([5, 1, 5, 2], [5, 2]) [0, 3] subsequence_indices("abc", "acb") [] subsequence_indices("abc", "acb", partial=True) [0, 2] subsequence_indices("abc", "bac", from_right=True, partial=True) [0, 2]
Source code in pydsa/seq.mu
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
run_lengths ¶
run_lengths(
xs: Sequence[Any],
extends: Callable[[Any, Any], bool],
from_right: bool = False,
) -> list[int]
The length of the run that ends at each index of xs: one value per item. A run is a stretch of items in which every pair of neighbours a, b satisfies extends(a, b). An item whose left neighbour fails extends(a, b) starts a new run of length 1.
from_right gives the length of the run that starts at each index instead. extends still takes the pair in index order, left item first.
run_lengths([5, 3, 3, 4, 2], (a, b) -> a >= b) [1, 2, 3, 1, 2] run_lengths([5, 3, 3, 4, 2], (a, b) -> a <= b, from_right=True) [1, 3, 2, 1, 1] run_lengths([1, 2, 3, 9, 10], (a, b) -> b - a == 1) [1, 2, 3, 1, 2] run_lengths("aab", (a, b) -> a == b) [1, 2, 1] run_lengths([], (a, b) -> a < b) []
Source code in pydsa/seq.mu
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |