我有:
@pakker[0] = 6
@number = [1, 2, 3, 4, 5, 6]
(1..@pakker[0]).map {|i| @number.map{|d| d}.combination(i).to_a}
在我的控制台中:
<r[0]).map {|i| @number.map{|d| d}.combination(i).to_a}
=> [[[1], [2], [3], [4], [5], [6]], [[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]], [[
1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1,
4, 5], [1, 4, 6], [1, 5, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4,
6], [2, 5, 6], [3, 4, 5], [3, 4, 6], [3, 5, 6], [4, 5, 6]], [[1, 2, 3, 4], [1, 2
, 3, 5], [1, 2, 3, 6], [1, 2, 4, 5], [1, 2, 4, 6], [1, 2, 5, 6], [1, 3, 4, 5], [
1, 3, 4, 6], [1, 3, 5, 6], [1, 4, 5, 6], [2, 3, 4, 5], [2, 3, 4, 6], [2, 3, 5, 6
], [2, 4, 5, 6], [3, 4, 5, 6]], [[1, 2, 3, 4, 5], [1, 2, 3, 4, 6], [1, 2, 3, 5,
6], [1, 2, 4, 5, 6], [1, 3, 4, 5, 6], [2, 3, 4, 5, 6]], [[1, 2, 3, 4, 5, 6]]]
irb(main):165:0>
相反,我不想要所有这些数组而是我想要的东西:
=> [1, 2, 3, 4, 5, 6, [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], etc.
如何删除数组中的所有数组?
我试过压扁:
<r[0]).map {|i| @number.map{|d| d}.combination(i).to_a}.flatten(1)
=> [[1], [2], [3], [4], [5], [6], [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], [1, 2,
3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 4, 5]
, [1, 4, 6], [1, 5, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6], [
2, 5, 6], [3, 4, 5], [3, 4, 6], [3, 5, 6], [4, 5, 6], [1, 2, 3, 4], [1, 2, 3, 5]
, [1, 2, 3, 6], [1, 2, 4, 5], [1, 2, 4, 6], [1, 2, 5, 6], [1, 3, 4, 5], [1, 3, 4
, 6], [1, 3, 5, 6], [1, 4, 5, 6], [2, 3, 4, 5], [2, 3, 4, 6], [2, 3, 5, 6], [2,
4, 5, 6], [3, 4, 5, 6], [1, 2, 3, 4, 5], [1, 2, 3, 4, 6], [1, 2, 3, 5, 6], [1, 2
, 4, 5, 6], [1, 3, 4, 5, 6], [2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
irb(main):197:0>
答案 0 :(得分:0)
必须有更好的方法,但如果a
是上述结果:
a.flatten(1).collect { |arr| arr.size == 1 ? arr[0] : arr }
此外,我认为以下内容相当于您的组合创建(删除内部map
):
(1..6).map { |i| number.map { |d| d }.combination(i) }
不是100%肯定。