【问题标题】:Trying to create a pair generator尝试创建对生成器
【发布时间】: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


【解决方案1】:

这是一个循环赛,每个玩家都与其他玩家比赛。像下面这样排列球员,组成对子 1 4、2 5 和 3 6:

123
456

修复播放器1,轮换剩余播放器:

142
563

产生对 1 5、4 6 和 2 3。保持旋转:

154
632

165
324

136
245

【讨论】:

    【解决方案2】:

    认为您只是在使用返回枚举器的内置combination 方法。您可以使用.to_a 将其变成一组独特的组合。

    [1, 2, 3, 4, 5, 6].combination(2).to_a
    # => [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6], [4, 5], [4, 6], [5, 6]]
    

    【讨论】:

    • 我认为他还希望将它们分成 5 组,每组 3 对不重叠。
    【解决方案3】:

    这称为1-factorization。完整图在 6 个顶点 {0,1,2,3,4,oo} 上的一个 1 分解是让第 i 天的时间表为 {{oo,i},{i+1,i+4}, {i+2,i+3}} 其中所有的数字 i+j 都减少了 mod 5。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-15
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 2012-03-01
      • 2011-12-01
      相关资源
      最近更新 更多