First, a helper function adapted from the itertools pairing recipe to create substrings.
import itertools def n_wise(iterable, n = 2): '''n = 2 -> (s0,s1), (s1,s2), (s2, s3), ... n = 3 -> (s0,s1, s2), (s1,s2, s3), (s2, s3, s4), ...''' a = itertools.tee(iterable, n) for x, thing in enumerate(a[1:]): for _ in range(x+1): next(thing, None) return zip(*a)
The function then iterates over the substrings, the longest at first, and tests for membership. (performance not taken into account)
def foo(s1, s2): '''Finds the longest matching substring '''
>>> domain >>>
wwii Aug 12 '16 at 7:02 2016-08-12 07:02
source share