我有一个个人项目,我想找出在特定Pisano序列中发现的模式每个序列(例如pisano(3):0、1、1、2、0、2、1、0、1、2、0、0、2、2、1、0、0、1、1、2、2、1、0……)都有一个重复模式,我要提取。在这个例子中,模式应该是(0,1,1,2,0,2,2,1)。
到目前为止,我已经研究了一种算法,它可以正常工作,除非序列的第一个数字在模式中找到。

def hasPattern(seq):
    pBuffer = []
    predict = ''
    i = 0
    check = True
    iSeq = 0
    passThrough = 0
    while (check == True) and passThrough <10:
        val = seq[iSeq]

        if predict == val: #how to resolve the duplicates pattern values? (1, 1, 3, 1, 1, 3)
        if iSeq == len(seq)-1:
            check = False
        if i < len(pBuffer)-1:
            i += 1
        else:
            i = 0
        else:
        i = 0
        iSeq = -1
        passThrough += 1
        pBuffer.append(val)

        predict = pBuffer[i]

        iSeq += 1

        if iSeq == len(seq)-1:
        check = False

    return {'pattern': pBuffer, 'size': len(pBuffer)}

我的问题是:我应该如何构建一个算法来检查任何序列中的重复模式(不仅仅是Pisano序列)?

最佳答案

p = [0, 1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2, 0, 2, 2, 1]

for b in range(2, len(p)/2):
    iterar = True
    for i in range(0,lp-b, b):
        if p[i:i+b]!=p[i+b:i+b+b]:
            iterar = False
            break

    if iterar:
        print "Solution %s: %s" %(b, p[:b])
        break

10-07 19:08
查看更多