返回一个可变长度的序列,其总和等于给定整数
采用 f(x,y,z)
形式,其中 x
是给定的整数和,y
是序列的最小长度,并且z
是序列的最大长度。但现在让我们假设我们正在处理一个固定长度的序列,因为否则我将花费很长时间来写这个问题。
所以我们的函数是 f(x,r) ,其中 x 是给定的整数和,r 是列表中序列的长度可能的序列。
对于 x = 10
和 r = 2
,这些是可能的组合:
1 + 9
2 + 8
3 + 7
4 + 6
5 + 5
让我们将其存储在 Python 中作为对列表:
[(1,9), (2,8), (3,7), (4,6), (5,5)]
因此用法如下:
>>> f(10,2)
[(1,9), (2,8), (3,7), (4,6), (5,5)]
回到原始状态问题,其中为 (y,x)
范围内的每个长度返回一个序列。我采用前面定义的 f(x,y,z)
形式,并省略了长度为 1
的序列(其中 yz == 0
) ,这看起来像:
>>> f(10,1,3)
[{1: [(1,9), (2,8), (3,7), (4,6), (5,5)],
2: [(1,1,8), (1,2,7), (1,3,6) ... (2,4,4) ...],
3: [(1,1,1,7) ...]}]
所以输出是一个字典列表,其中值是一个对列表。并不完全是最佳的。
所以我的问题是:
- 是否有一个库可以处理这个问题?
- 如果没有,有人可以帮我编写我提到的两个函数吗? (首先固定序列长度)?
- 由于我对相当琐碎的数学知识的巨大差距,您能否忽略我的整数存储方法并使用最有意义的结构?
对今天所有这些算术问题感到抱歉。谢谢!
In the form f(x,y,z)
where x
is a given integer sum, y
is the minimum length of the sequence, and z
is the maximum length of the sequence. But for now let's pretend we're dealing with a sequence of a fixed length, because it will take me a long time to write the question otherwise.
So our function is f(x,r)
where x
is a given integer sum and r
is the length of a sequence in the list of possible sequences.
For x = 10
, and r = 2
, these are the possible combinations:
1 + 9
2 + 8
3 + 7
4 + 6
5 + 5
Let's store that in Python as a list of pairs:
[(1,9), (2,8), (3,7), (4,6), (5,5)]
So usage looks like:
>>> f(10,2)
[(1,9), (2,8), (3,7), (4,6), (5,5)]
Back to the original question, where a sequence is return for each length in the range (y,x)
. I the form f(x,y,z)
, defined earlier, and leaving out sequences of length 1
(where y-z == 0
), this would look like:
>>> f(10,1,3)
[{1: [(1,9), (2,8), (3,7), (4,6), (5,5)],
2: [(1,1,8), (1,2,7), (1,3,6) ... (2,4,4) ...],
3: [(1,1,1,7) ...]}]
So the output is a list of dictionaries where the value is a list of pairs. Not exactly optimal.
So my questions are:
- Is there a library that handles this already?
- If not, can someone help me write both of the functions I mentioned? (fixed sequence length first)?
- Because of the huge gaps in my knowledge of fairly trivial math, could you ignore my approach to integer storage and use whatever structure the makes the most sense?
Sorry about all of these arithmetic questions today. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我们处理预突变时, itertools 模块肯定会很有帮助 - 然而,这看起来可疑地像一个家庭作业...
编辑:不过看起来很有趣,所以我会尝试一下。
编辑2:这就是你想要的吗?
输出:
The itertools module will definately be helpful as we're dealing with premutations - however, this looks suspiciously like a homework task...
Edit: Looks like fun though, so I'll do an attempt.
Edit 2: This what you want?
Output:
该问题称为整数分区,并且具有被广泛研究。
在这里您可以找到论文比较了几种算法的性能(并提出了一种特定的算法),但网络上有很多参考文献。
The problem is known as Integer Partitions, and has been widely studied.
Here you can find a paper comparing the performance of several algorithms (and proposing a particular one), but there are a lot of references all over the Net.
我刚刚编写了一个递归生成器函数,您应该弄清楚如何自己从中获取列表...
编辑:我只是看到它并不完全是您想要的,我的版本也会生成重复项,其中只有顺序不同。但是您可以通过对所有结果进行排序并检查重复的元组来简单地过滤掉它们。
I just wrote a recursive generator function, you should figure out how to get a list out of it yourself...
EDIT: I just see it is not exactly what you wanted, my version also generates duplicates where only the ordering differs. But you can simply filter them out by ordering all results and check for duplicate tuples.