Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 对数组进行分区_Ruby_Algorithm - Fatal编程技术网

Ruby 对数组进行分区

Ruby 对数组进行分区,ruby,algorithm,Ruby,Algorithm,我正在努力编写一个算法,将数组转换为该数组的所有可能排列,但它们必须是内联/连续的。示例如下: 数组: ['a', 'b', 'c', 'd', 'e', 'f'] 结果将是: [['a'], ['b', 'c', 'd', 'e', 'f']] [['a', 'b'], ['c', 'd', 'e', 'f']] [['a', 'b'], ['c', 'd'], ['e', 'f']] ... Array#排列创建排列,但排列不整齐 非常感谢您的帮助。根据我对您的问题的理解(这与其他人的理

我正在努力编写一个算法,将数组转换为该数组的所有可能排列,但它们必须是内联/连续的。示例如下:

数组:

['a', 'b', 'c', 'd', 'e', 'f']
结果将是:

[['a'], ['b', 'c', 'd', 'e', 'f']]
[['a', 'b'], ['c', 'd', 'e', 'f']]
[['a', 'b'], ['c', 'd'], ['e', 'f']]
...
Array#排列
创建排列,但排列不整齐


非常感谢您的帮助。

根据我对您的问题的理解(这与其他人的理解不同),您需要数组的每个可能分组(分区),其中数组的各个元素(字符)保持其顺序(总是
'a'
,然后
'b'
,然后
'c'
,…
'f'
)。我认为这是一个获取每个分区大小的有序列表集的问题

也就是说,我首先将您的三个示例划分表示为:

[[1, 5],
 [2, 4],
 [2, 2, 2],
 ...]
因此,我首先生成:

[[6], [1, 5], [2, 4], [3, 3] ...]
然后用它来生成最终结果

我生成大小的方法效率非常低。这是我想到的第一件事,它对您的阵列非常有效,但是如果需要处理更大的阵列,则需要更好的算法。(sawa现在提供了一个更短、更高效的解决方案。)

产生以下结果:

[[["a", "b", "c", "d", "e", "f"]],
 [["a"], ["b", "c", "d", "e", "f"]],
 [["a", "b"], ["c", "d", "e", "f"]],
 [["a", "b", "c"], ["d", "e", "f"]],
 [["a", "b", "c", "d"], ["e", "f"]],
 [["a", "b", "c", "d", "e"], ["f"]],
 [["a"], ["b"], ["c", "d", "e", "f"]],
 [["a"], ["b", "c"], ["d", "e", "f"]],
 [["a"], ["b", "c", "d"], ["e", "f"]],
 [["a"], ["b", "c", "d", "e"], ["f"]],
 [["a", "b"], ["c"], ["d", "e", "f"]],
 [["a", "b"], ["c", "d"], ["e", "f"]],
 [["a", "b"], ["c", "d", "e"], ["f"]],
 [["a", "b", "c"], ["d"], ["e", "f"]],
 [["a", "b", "c"], ["d", "e"], ["f"]],
 [["a", "b", "c", "d"], ["e"], ["f"]],
 [["a"], ["b"], ["c"], ["d", "e", "f"]],
 [["a"], ["b"], ["c", "d"], ["e", "f"]],
 [["a"], ["b"], ["c", "d", "e"], ["f"]],
 [["a"], ["b", "c"], ["d"], ["e", "f"]],
 [["a"], ["b", "c"], ["d", "e"], ["f"]],
 [["a"], ["b", "c", "d"], ["e"], ["f"]],
 [["a", "b"], ["c"], ["d"], ["e", "f"]],
 [["a", "b"], ["c"], ["d", "e"], ["f"]],
 [["a", "b"], ["c", "d"], ["e"], ["f"]],
 [["a", "b", "c"], ["d"], ["e"], ["f"]],
 [["a"], ["b"], ["c"], ["d"], ["e", "f"]],
 [["a"], ["b"], ["c"], ["d", "e"], ["f"]],
 [["a"], ["b"], ["c", "d"], ["e"], ["f"]],
 [["a"], ["b", "c"], ["d"], ["e"], ["f"]],
 [["a", "b"], ["c"], ["d"], ["e"], ["f"]],
 [["a"], ["b"], ["c"], ["d"], ["e"], ["f"]]]

如果我理解正确的话,这正是您所追求的。

正如我理解您的问题(这与其他人理解它的方式不同),您希望数组的每个可能的分组(分区),其中数组的各个元素(字符)保持其顺序(总是
'a'
,然后
'b'
,然后
'c'
,…
'f'
)。我认为这是一个获取每个分区大小的有序列表集的问题

也就是说,我首先将您的三个示例划分表示为:

[[1, 5],
 [2, 4],
 [2, 2, 2],
 ...]
因此,我首先生成:

[[6], [1, 5], [2, 4], [3, 3] ...]
然后用它来生成最终结果

我生成大小的方法效率非常低。这是我想到的第一件事,它对您的阵列非常有效,但是如果需要处理更大的阵列,则需要更好的算法。(sawa现在提供了一个更短、更高效的解决方案。)

产生以下结果:

[[["a", "b", "c", "d", "e", "f"]],
 [["a"], ["b", "c", "d", "e", "f"]],
 [["a", "b"], ["c", "d", "e", "f"]],
 [["a", "b", "c"], ["d", "e", "f"]],
 [["a", "b", "c", "d"], ["e", "f"]],
 [["a", "b", "c", "d", "e"], ["f"]],
 [["a"], ["b"], ["c", "d", "e", "f"]],
 [["a"], ["b", "c"], ["d", "e", "f"]],
 [["a"], ["b", "c", "d"], ["e", "f"]],
 [["a"], ["b", "c", "d", "e"], ["f"]],
 [["a", "b"], ["c"], ["d", "e", "f"]],
 [["a", "b"], ["c", "d"], ["e", "f"]],
 [["a", "b"], ["c", "d", "e"], ["f"]],
 [["a", "b", "c"], ["d"], ["e", "f"]],
 [["a", "b", "c"], ["d", "e"], ["f"]],
 [["a", "b", "c", "d"], ["e"], ["f"]],
 [["a"], ["b"], ["c"], ["d", "e", "f"]],
 [["a"], ["b"], ["c", "d"], ["e", "f"]],
 [["a"], ["b"], ["c", "d", "e"], ["f"]],
 [["a"], ["b", "c"], ["d"], ["e", "f"]],
 [["a"], ["b", "c"], ["d", "e"], ["f"]],
 [["a"], ["b", "c", "d"], ["e"], ["f"]],
 [["a", "b"], ["c"], ["d"], ["e", "f"]],
 [["a", "b"], ["c"], ["d", "e"], ["f"]],
 [["a", "b"], ["c", "d"], ["e"], ["f"]],
 [["a", "b", "c"], ["d"], ["e"], ["f"]],
 [["a"], ["b"], ["c"], ["d"], ["e", "f"]],
 [["a"], ["b"], ["c"], ["d", "e"], ["f"]],
 [["a"], ["b"], ["c", "d"], ["e"], ["f"]],
 [["a"], ["b", "c"], ["d"], ["e"], ["f"]],
 [["a", "b"], ["c"], ["d"], ["e"], ["f"]],
 [["a"], ["b"], ["c"], ["d"], ["e"], ["f"]]]

如果我理解正确,这正是您所追求的。

如果您想要数组中所有可能排列的所有可能组合,则生成排列,然后生成它们的切片:

array = %w(a b c d e f)
r = array.permutation(array.length).map {|perm|
  perm.length.times.map {|i| perm.each_slice(i+1).to_a }
}.flatten(1)

这将生成输入数组的每个可能排列的每个可能分组。不过,我不太确定这是您要查找的内容。

如果您想要数组的所有可能排列的所有可能组合,则生成排列,然后生成它们的切片:

array = %w(a b c d e f)
r = array.permutation(array.length).map {|perm|
  perm.length.times.map {|i| perm.each_slice(i+1).to_a }
}.flatten(1)
a = ['a', 'b', 'c', 'd', 'e', 'f']
(0...a.length)
.flat_map{|i| (1...a.length).to_a.combination(i).to_a}
.map{|cut| i = -1; a.slice_before{cut.include?(i +=1)}.to_a}


这将生成输入数组的每个可能排列的每个可能分组。不过,我不太确定这是您要查找的。

您所说的“顺序”是什么意思?您想要的顺序是什么?我使用的是“排列”一词但我可能是误用了。
Array#permutations
提供了所有可能的置换。我要寻找的是所有可能的数组“permutations”,正如输出所示。您的输出不是置换。它是分区。@ArtemKalinchuk请清楚您想要什么操作。我不确定“操作”是什么调用。我想如果我知道算法,我可能会找到它。我提供了一个例子来帮助解决。你所说的“顺序”是什么意思?你想要的顺序是什么?我使用了“排列”这个术语,但我可能误用了。
Array#permutations
提供了所有可能的排列。我正在寻找的是所有可能的数组如输出所示的“置换”。您的输出不是置换。它是分区。@ArtemKalinchuk请清楚您想要什么操作。我不确定“操作”是什么"是的。我想如果我知道算法的话,我可能会找到答案。我提供了一个例子来帮助。这也将生成所有可能的排列顺序。是的。我选择了它,因为它涵盖了所有情况,并且原始问题的期望输出集不清楚。:d如果你不想排序,这是非常困难的重复。我已经挣扎了大约半个小时。这很有效,但我要赞扬@sawa,因为他的解决方案更快。这也将生成所有可能的排列顺序。是的。我选择了它,因为它涵盖了所有情况,并且原始问题的期望输出集不清楚。:d如果你愿意,这是非常困难的我不想要有序的重复。我已经挣扎了大约半个小时。这很有效,但我要赞扬@sawa,因为他的解决方案更快。正如你可能已经发现的,我关注的是指数,而你关注的是长度。它们是有趣的方式。哇,我昨天花了一个小时试图找到一个简单的解决方案就像这个。@sawa非常感谢你!这正是我想要的,但我不知道怎么做。Jeffrey我可能比你花了更多的时间:)正如你可能已经发现的那样,我关注的是指数,而你关注的是长度。它们是有趣的方式。哇,我昨天花了一个小时试图找到一个像这样的简单解决方案。@sawa非常感谢!这正是我想要的,但却不知道如何做。Jeffrey,我可能花了更多的时间时间比你长:)
a = ['a', 'b', 'c', 'd', 'e', 'f']
(0...a.length)
.flat_map{|i| (1...a.length).to_a.combination(i).to_a}
.map{|cut| i = -1; a.slice_before{cut.include?(i +=1)}.to_a}