Best strategy for finding a pattern in a sequence of integers

Paul McGuire ptmcg at austin.rr.com
Fri Nov 21 20:40:19 EST 2008


So I think you just need to find the first two complete sequences of
1,6,10 and 0,3,9, remove any repetitions and then you're done.

data = '''
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 7 3 0 3 3 0 3 3 0 10 6 6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 10 6 6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6
6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6
6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1'''


data = [int(x) for x in data.split()]

s1 = frozenset([1,6,10])
s2 = frozenset([0,3,9])

diter = iter(data)

i = diter.next()
curset = (s1,s2)[i in s2]
otherset = lambda : (s1,s2)[curset is s1]
seq = { s1 : [], s2 : [] }

# read until there is the first change in state - discard
# these, since we may have started in the middle of a sequence
other = otherset()
while i not in other:
    i = diter.next()

# read in 2 sequences
for _ in range(2):
    other = curset
    curset = otherset()
    tmp = []
    while i not in other:
        if i in curset:
            tmp.append(i)
        i = diter.next()
    seq[curset] = tmp[:]

# look for repeats in a seq, truncate
def truncateReps(s,sentinel):
    if s.count(sentinel) > 1:
        loc1 = s.index(sentinel)
        loc2 = s.index(sentinel,loc1+1)
        s[:] = s[:loc2-loc1]

truncateReps(seq[s1],10)
truncateReps(seq[s2],9)

# the answer
print seq[s1]
print seq[s2]

Prints:
[10, 6, 6, 1, 6, 6, 1, 6, 6, 1, 6, 6, 1, 6, 6, 1, 6, 6, 1, 6, 6, 1]
[9, 3, 3, 0, 3, 3, 0, 3, 3, 0, 3, 3, 0]

Your original sample was only the nominal, most friendly case, so it
is hard to know if any submitted solutions will work will all of your
other conditions.  Please try this with more challenging data, such as
starting a sequence in the middle, numbers not in the set
(0,1,3,6,9,10), repeated patterns, sequences that don't start with 9
or 10, etc.

-- Paul



More information about the Python-list mailing list