Skip to content

Instantly share code, notes, and snippets.

@johnw42
Last active October 21, 2022 17:03
Show Gist options
  • Save johnw42/44ad2c244e500c19385e8f72f04a1a20 to your computer and use it in GitHub Desktop.
Save johnw42/44ad2c244e500c19385e8f72f04a1a20 to your computer and use it in GitHub Desktop.
from itertools import takewhile
from typing import Iterable, Sequence, TypeVar
T = TypeVar("T")
def common_prefix(seqs: Iterable[Sequence[T]]) -> Iterable[T]:
"Returns the common prefix of a sequence of sequences."
return (c[0] for c in takewhile(lambda x: all(x[0] == y for y in x), zip(*seqs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment