Source code for scicomp.exam.kk.kmp

from typing import Iterable, Optional, Sequence, TypeVar


__all__ = 'kmp_search',


_T = TypeVar('_T')


def _kmp_table(needle: Sequence[_T]) -> Sequence[int]:
    """Compute the KMP partial match "table"."""
    return [j for j in [-1]
            for cur in needle  # linear scan of the needle
            for j in [j+1 if cur == needle[j] else 0]]