Python 在列表中查找模式的周期

Python 在列表中查找模式的周期,python,intervals,fibonacci,period,Python,Intervals,Fibonacci,Period,我对编码比较陌生,但我看到了一个很好的例子,他们使用斐波那契序列模的特定重复模式将音调分配给结果数字。这是一个多么伟大的小实验来测试我的知识 因此,我能够创建一个简单的循环来创建斐波那契序列的列表,并创建另一个函数来计算所生成序列除以n后的剩余部分。但在模量列表中找到模式的周期被证明是困难的 以下是我目前掌握的情况: #Fibonacci.py #Basic terms fiblist = list() inp = "> " n=None #Calculates the FIbonacc

我对编码比较陌生,但我看到了一个很好的例子,他们使用斐波那契序列模的特定重复模式将音调分配给结果数字。这是一个多么伟大的小实验来测试我的知识

因此,我能够创建一个简单的循环来创建斐波那契序列的列表,并创建另一个函数来计算所生成序列除以n后的剩余部分。但在模量列表中找到模式的周期被证明是困难的

以下是我目前掌握的情况:

#Fibonacci.py
#Basic terms
fiblist = list()
inp = "> "
n=None

#Calculates the FIbonacci sequence
def fib(n):
    a,b = 0,1
    while True:
        try:
            print "How many terms? "
            n = int(raw_input(inp))
            if n <= 0:
                print "Please enter a positive integer."
                continue
            else:
                for i in range(0,n):
                    a,b = b, a + b
                    fiblist.append(a)
                break
        except ValueError:
            print "Please enter a positive integer, not a letter or symbol"
            continue    
    return fiblist

#Calculates the modulo of each integer in fiblist
def modulo(n):  
    print """
Do you want to find the modulo?
1. Yes
2. No"""
    choice = raw_input(inp)
    if choice =="1":
        modlist = list()
        print "What modulo do you want to use?"
        modx = int(raw_input(inp))
        modlist = [x % modx for x in fiblist]
        print modlist
        print "The period of the pattern is ", principal_period(modlist)
        print "Goodbye!"
    else: 
        print "Goodbye!"

#Calculates the period of the modulo pattern of the Fibonacci sequence
def principal_period(modlist):
    a = str(modlist)
    i = (a+a).find(a, 1, -1)
    return None if i == -1 else n[:i]

print fib(n)
modulo(n)
它总是返回“无”,我从关于答案的线程中得到了这个。老实说,我不太明白这个答案,它没有给我想要的结果


您对计算给定列表中重复模式的周期有什么建议吗?

它失败了,因为
modlist
是一个整数列表,而不是字符串。将主周期中的第二行和第四行更改如下:

def principal_period(modlist):
    a = ''.join(str(m) for m in modlist)
    i = (a+a).find(a, 1, -1)
    return None if i == -1 else a[:i]
而不是
str(modlist)
你需要把数字连接成一个长字符串,你在最后一行有一个打字错误,
n[:i]
而不是
a[:i]

现在运行此程序将显示:

How many terms? 
> 9
[1, 1, 2, 3, 5, 8, 13, 21, 34]

Do you want to find the modulo?
1. Yes
2. No
> 1
What modulo do you want to use?
> 2
[1, 1, 0, 1, 1, 0, 1, 1, 0]
The period of the pattern is  110
Goodbye!

这个输出的有趣之处在于斐波那契数遵循奇数、奇数、偶数序列。试试99条款

这里有一个简单的算法来计算离散函数的周期:

def findperiod(f,最小长度=1,重复次数=2):
“”“查找函数f的周期。”。
想法:假设长度i=1。检查第二份副本是否与第一份相同(j
如果不是,则假定长度为2。以此类推。一旦第二个副本匹配
首先(无j断开),返回假定长度。
可选的重复参数可以增加,以检查是否有更多重复
比默认值重复2次(开始时相同序列的两个副本
(名单的第二部分)。
如果在前十亿个值中没有找到足够的重复,则返回None。
"""
对于i(范围(最小长度,2**30)):
对于范围内的重复(1,重复次数):
对于范围(i)中的j:
如果f(j)!=f(i*rep+j):中断
其他:返回我

如果你的函数计算起来很昂贵,那么记忆是个好主意。我在这里没有这样做,因为如果没有必要,它会占用大量内存。

这对我来说不起作用。我按照你的建议修改了代码,但在这段时间里仍然没有收到任何回复。它在任何条件下都不起作用。你试过9和99了吗?这对9和99是有效的,但不是对所有的条款都有效。我还能够找到len(principal_period(modlist))来获得模式重复之前modlist中的术语数量。这是我一直在寻找的时期。然而,当我使用像12这样的术语时,它为我提供了一个None,并对len(None)进行了回溯。此外,此解决方案似乎仅在modx=2时有效。注释中的所有内容都是“是”,除了90个术语(模4)返回的模式为112310。模式必须完全重复(89或91个术语太少/太多)。这是《守则》的一项要求,比我以前的守则更实用。但我仍然希望能够找到每个病例的模式周期。我的意思是,我能在所有这些案例中直观地找到它。假设我选择项=20,模=3,周期应该是8,因为modlist=[1,1,2,0,2,2,1,0,1,1,1,2,0,2,2,1,0,1,1,2,0]。
How many terms? 
> 9
[1, 1, 2, 3, 5, 8, 13, 21, 34]

Do you want to find the modulo?
1. Yes
2. No
> 1
What modulo do you want to use?
> 2
[1, 1, 0, 1, 1, 0, 1, 1, 0]
The period of the pattern is  110
Goodbye!