Closed. This question is off-topic. It is not currently accepting answers. Learn more。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
我一直在想如何让某个函数在python中工作。函数将正整数转换为其他正整数,如下所示:
Phi_m(n2) = Phi_m(m*n + r) = m*x[n] + r*(x[n + 1] - x[n])
以上术语均为整数值,定义如下:
n2 = the (n2)th slot of the output string
m = a fixed positive integer
n = some multiple of m such that n*m is less than or equal to n2
r = a remainder term to fill in the amount missing from n*m in decomposing n2
x[n] = the element in the [n1]th slot of the input string
x[n + 1] = the element in the [n1 + 1]th slot of the input string
一般来说,我们从一串数字开始,比如0,1,1,2,3,3,最后是一串(k+1)m-1项,其中k是开始的项的数目,不包括0。要使用这个函数,我们首先确定m,比如m=2。现在我们用m分解n2,其中n2表示输出序列的一个“时隙”假设n2=5。然后我们问‘输出字符串的第五个‘槽’中有什么’在这种情况下,我们的总输出字符串的长度为(5+1)2+1。请注意,我们不计算0-它总是存在的,并且是为我们的目的第0期,因此我们有5个初始期。为了回答这个问题,我们把5=2*2+1作为我们的分解。现在我们已经分解了,我们可以应用我们的函数:
F(x(5)) = F(x(2*2+1)) 2x[2] + 1(x[3] - x[2]).
问题是,python要做到这一点,必须知道如何分解每个数字。所以它知道2是固定的,知道2*3太多,所以选择2*2。然后它必须知道这是太少了,并加上余数1。只有这样做了,它才能真正抓取n=5。也就是说,它可以运行函数。很明显,一旦它知道如何实现这个功能,它就可以遍历我们范围内的每一个n,但我真的不知道如何编程这个函数的肉。
现在回答几个问题:x是函数吗名单一个数字?x[n]本质上是一个列表。
当你说“输入字符串的值”是什么意思?菲姆的签名是什么?
作用在这个列表上的函数接受列表中的一个元素,以某种方式对数字进行分解,然后应用上面看到的“公式”从这个意义上说,它更像是一个两步算法。
如果不清楚,请告诉我。我一定会继续修改,直到对那些阅读者有意义为止。
也许这段代码能让你接近答案:
>>> def phi_m(x, m):
... rtn = []
... for n2 in range(0, len(x) * m - 2:
... n = n2 / m
... r = n2 - n * m
... rtn.append(m * x[n] + r * (x[n + 1] - x[n]))
... print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn
... rtn
...
>>> x = [0, 1, 1, 2, 3, 3]
>>> phi_m(x, 2)
n2 = 0 : n = 0 r = 0 rtn = [0]
n2 = 1 : n = 0 r = 1 rtn = [0, 1]
n2 = 2 : n = 1 r = 0 rtn = [0, 1, 2]
n2 = 3 : n = 1 r = 1 rtn = [0, 1, 2, 2]
n2 = 4 : n = 2 r = 0 rtn = [0, 1, 2, 2, 2]
n2 = 5 : n = 2 r = 1 rtn = [0, 1, 2, 2, 2, 3]
n2 = 6 : n = 3 r = 0 rtn = [0, 1, 2, 2, 2, 3, 4]
n2 = 7 : n = 3 r = 1 rtn = [0, 1, 2, 2, 2, 3, 4, 5]
n2 = 8 : n = 4 r = 0 rtn = [0, 1, 2, 2, 2, 3, 4, 5, 6]
n2 = 9 : n = 4 r = 1 rtn = [0, 1, 2, 2, 2, 3, 4, 5, 6, 6]
>>>
你最初的长度公式产生了一个超出范围的错误,就像你评论中的那个一样。你的意思显然是。