分解函数

2024-04-18 21:58:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

其中n2和n1是输入字符串的两个不同值,其中n1是通过“分解”n2得到的。我们考虑x[0] = 0r始终为零或正且小于m,并且{}的所有值(其中任何一个)都是正整数。一般来说,functional接受一个数字串并输出另一个字符串。通常情况下,我们修复一个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。在

我最初想做的是:

^{pr2}$

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

关键是,Python要做到这一点,就必须知道如何分解每个数字。所以它知道2是固定的,并且知道{}太多,所以选择{}。然后它必须知道这太少了,然后加上余数1。只有完成后,它才能真正抓取n = 5。也就是说,它可以运行函数。很明显,一旦它知道如何做到这一点,它就可以遍历我们范围内的每一个n,但我真的不确定如何编写这个函数的核心。在


Tags: 函数字符串元素示例列表情况序列phi
1条回答
网友
1楼 · 发布于 2024-04-18 21:58:30

下面是我如何分解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相同:

^{pr2}$

仅使用cd4{或更多:

>>> 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
>>> 
>>> 

相关问题 更多 >