1

我一直对如何让某个函数在 Python 中工作感到困惑。函数本身是这样的:

(Phi_m)x((n2)) = (Phi_m)(x(m*n + r)) = m*x[n1] + r*(x([n1] + 1) - x[n1])

注意:这里的 n 只是指定一些倍数。它不是一个列表元素,但是当应用 x 时它变成了一个列表元素。在下面的示例中,例如,我们可能认为 n 大于列表中的任何元素。例如,一个列表有 9 个元素,最大的是 3 个,并且 m=1 - 这里 n=9 =/= 列表的元素。

其中 n2 和 n1 是输入字符串的两个不同值,其中 n1 是通过“分解”n2 导出的。我们认为x[0] = 0,r总是为零或正且小于m,并且(其中任何一个)的所有值n都是正整数。一般来说,函数式接受一串数字并输出另一个字符串。通常发生的是我们修复一个m,比如说m = 2。现在我们分解n2. 说n2 = 5。然后F(x(5)) = F(x(2*2+1)) 2x[2] + 1(x[3] - x[2])0, 1, 1, 2, 3, 3因此,如果我们的完整输入序列是2*1+0=2. 所以我们的第五个输出项是2

我最初想做类似的事情:

x = [0,1,1,2,3,3,3,3]

def F(n,j,r,x):
return j * x[n] + r(x[n + 1] - x[n])
for n in range(len(x) - 1):
    print n

但这显然不符合我的目的。

问题是,要让 Python 做到这一点,它必须知道如何分解每个数字。所以它知道2是固定的,知道2*3太多所以选择2*2。然后它必须知道这太少并添加剩余部分1。只有做到这一点,它才能真正抓住n = 5. 也就是说,它可以运行该功能。似乎很清楚,一旦它知道如何执行此操作,它就可以遍历n我们范围内的每一个,但我真的不确定如何编写这个函数的核心。

4

1 回答 1

1

以下是我如何以以下形式分解数字n2 = m * n1 + r

>>> def decompose(number):
...     # returns a generator of tuples (m, n1, r)
...     for m in range(1, number+1):
...         yield m, number // m, number % m
... 
>>> for m, n1, r in decompose(5):
...     print "5 = %s * %s + %s" % (m, n1, r)
... 
5 = 1 * 5 + 0
5 = 2 * 2 + 1
5 = 3 * 1 + 2
5 = 4 * 1 + 1
5 = 5 * 1 + 0

或使用固定m,这与常规相同divmod

>>> def decompose(number):
...     return number // m, number % m
... 
>>> m = 2
>>> n1, r = decompose(5)
>>> print "5 = %s * %s + %s" % (m, n1, r)
5 = 2 * 2 + 1
>>> m = 4
>>> n1, r = decompose(5)
>>> print "5 = %s * %s + %s" % (m, n1, r)
5 = 4 * 1 + 1

或更简单地使用lambda

>>> decompose = lambda number: divmod(number, m)
>>> 
>>> m = 2
>>> decompose(5)
(2, 1)
>>> m = 4
>>> decompose(5)
(1, 1)

现在,举一个完整的例子:

>>> decompose = lambda number: divmod(number, m)
>>> 
>>> class Phi_m(list):
...     def __init__(self, items):
...         list.__init__(self)
...         # you need to know at least m numbers.
...         assert len(items) >= m, 'Not enough data'
...         list.extend(self, items)
...     # this is a sparse list
...     # http://stackoverflow.com/questions/1857780/sparse-assignment-list-in-python
...     def __setitem__(self, index, value):
...         missing = index - len(self) + 1
...         if missing > 0:
...             self.extend([None] * missing)
...             list.__setitem__(self, index, value)
...     def __getitem__(self, index):
...         try:
...             value = list.__getitem__(self, index)
...             if value is not None:
...                 # the item is in the list, yeah!
...                 return value
...             # the item is in the list because it was resized
...             # but it is None, so go on and calculate it. 
...         except IndexError:
...             # the item is not in the list, calculate it.
...             pass
...         print 'calculating Fm[%s]' % index
...         A, B = decompose(index)
...         value1 = self.__getitem__(A)
...         value2 = self.__getitem__(A + 1)
...         print 'Fm[A=%s] = %s, Fm[A+1=%s] = %s' % (A, value1, A+1, value2)
...         print 'back to calculating Fm[%s]' % index
...         # m * x[n1] + r * (x[n1 + 1] - x[n1]) = (m - r) * x[n1] + r * x[n1 + 1]
...         # A = n1 ; B = r ; value1 = x[n1] ; value2 = x[n+1]
...         value = (m - B) * value1 + B * value2
...         self.__setitem__(index, value)
...         return value
... 
>>> x = Phi_m([0, 1, 1])
>>> 
>>> x[5]
calculating Fm[5]
calculating Fm[3]
Fm[A=1] = 1, Fm[A+1=2] = 1
back to calculating Fm[3]
Fm[A=2] = 1, Fm[A+1=3] = 2
back to calculating Fm[5]
3
>>> 
>>> 
于 2013-07-16T01:41:34.800 回答