植原といいます。

"しん" <dezawa / aliadne.net> wrote:
> 
>  [1,2,3,4].conbination(2)   => [ [1,2],[1,3],[1,4],[2,3],[2,4],[3,4] ]
>  [1,2,3].conbination([4,5]) => [ [1,4],[1,5],[2,4],[2,5],[3,4],[3,5] ]
> 
> こんな結果を帰すものを ruby的に作るにはどうしたものでしょうか。
> 

2番目の方は集合の直積(direct product)という感じなので、
product というメソッド名の方がよいように思いました。


class Array
  def combination(num)
    return [] if num < 1 || num > size
    return map{|e| [e] } if num == 1
    tmp = self.dup
    self[0, size - (num - 1)].inject([]) do |ret, e|
      tmp.shift
      ret += tmp.combination(num - 1).map{|a| a.unshift(e) }
    end
  end

  def product(other)
    inject([]) do |ret, es|
      ret += other.map{|eo| [es, eo]}
    end
  end
end


require 'test/unit'
class MyArrayTest < Test::Unit::TestCase
  def test_combination
    assert_equal([], [1,2,3,4].combination(0))
    assert_equal([ [1],[2],[3],[4] ], [1,2,3,4].combination(1))
    assert_equal([ [1,2],[1,3],[1,4],[2,3],[2,4],[3,4] ], [1,2,3,4].combination(2))
    assert_equal([ [1,2,3],[1,2,4],[1,3,4],[2,3,4] ], [1,2,3,4].combination(3))
    assert_equal([ [1,2,3,4] ], [1,2,3,4].combination(4))
    assert_equal([], [1,2,3,4].combination(5))
  end

  def test_product
    assert_equal([ [1,4],[1,5],[2,4],[2,5],[3,4],[3,5] ], [1,2,3].product([4,5]))
    assert_equal([ [1,1],[1,2],[2,1],[2,2] ], [1,2].product([1,2]))
  end
end


--------------------------------
植原祥之 ( yuehara / ainix.isac.co.jp )