【发布时间】:2015-09-08 17:48:16
【问题描述】:
我正在尝试构建一个配对生成器。它需要一个包含六个名称的列表,并为一周(5 天)生成对,尽可能少地复制。
我将复制的配对减少到最少 2 对(所以我找到了 5 天的配对,即总共 15 对组合,只有 2 组相同)。
我的方法:
# Start with individuals in an array
[1, 2, 3, 4, 5, 6]
# Bisect the array
[1, 2, 3]
[4, 5, 6] => yields pair combinations [1, 4], [2, 5], [3, 6]
# Move the lower of the bisected arrays along
[1, 2, 3]
[6, 4, 5] => yields pair combinations [1, 6], [2, 4], [3, 5]
# Move along once more
[1, 2, 3]
[5, 6, 4] => yields pair combinations [1, 5], [2, 6], [3, 4]
# Since there are no more unique pair combinations, bisect each array again
(Array 1) [1, 2]
(Array 1) [3] => yields pair combination [1, 3] with 2 'spare'
(Array 2) [4, 5]
(Array 2) [6] => yields pair combination [4, 6] with 6 'spare'
=> 'spare' pair combination [2, 6] is a replication
# Move the lower of the bisected arrays along
(Array 1) [1, 2]
(Array 1) [3] => yields pair combination [2, 3] with 1 'spare'
(Array 2) [4, 5]
(Array 2) [6] => yields pair combination [5, 6] with 4 'spare'
=> 'spare' pair combination [1, 4] is a replication
上述过程为我们提供了 13 个唯一对,然后是 2 个非唯一对。涵盖了一周中的每一天,但我们会复制。
有没有什么方法可以更有效/避免复制?
【问题讨论】:
-
抱歉,我很难理解您的问题。您是否在问如何从 6 个元素中生成尽可能多的非重复对?
-
是的,我真的不确定你在问什么。我无法遵循上述过程,或者“备用”是什么。您能否根据
[1,2,3,4,5,6]的输入向我们提供您所追求的清晰明确的输出? -
我在这里有一个相关的问题:math.stackexchange.com/questions/1477767/…
标签: ruby algorithm math combinations permutation