ruby - 尝试创建一个对生成器

标签 ruby algorithm math combinations permutation

我正在尝试构建一个配对生成器。它需要六个名称的列表,并生成一周(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 个非唯一对。一周中的每一天都会被覆盖,但我们会复制。

有什么方法可以更有效地做到这一点/避免复制吗?

最佳答案

这是一场循环赛,每个玩家都与其他玩家进行比赛。将玩家如下所示排列成 1 4、2 5 和 3 6 组:

123
456

固定球员1,轮换其余球员:

142
563

生成对 1 5、4 6 和 2 3. 继续旋转:

154
第632章

165
324

136
245

关于ruby - 尝试创建一个对生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32464154/

相关文章:

mysql - Ruby on Rails 中的复选框数组

javascript - for 循环在 while 循环内 - o(n) 平方?

javascript - 将时间增量和速度纳入速度/摩擦运动

math - 对于 Lisp 初学者来说,有什么好的数学书籍?

ruby-on-rails - Rails 关联在控制台中有效,但在 View 中无效

IO :Class (NoMethodError) 的 Ruby 未定义方法 `write'

ruby - 检查一个值是否小于数组的任何值

arrays - 我们如何根据运行时性能在两种排序算法之间切换?

c - 如何计算范围号 1 到 n 中 0 或 1 的总出现次数?

c# - 在特定条件下使用 LINQ 从列表中获取数字