Recursive method for generation of combination
갑자기 한 번 짜봤는데 성능에는 문제가 많습니다.
그냥 combination 생성 함수를 재귀로 만들면 짧게 되는 지 보기 위해서...
module  Combination
   def self.generate(list, size)
       if size == 1
           list.map {|e| Array.new.push e }
       else
           total = Array.new
           list.each do |x|
               smaller_list = list - Array.new.push(x)
               partial  = self.generate(smaller_list, size-1).map do |y|
                   y.push x if y.last < x
               end
               total.concat partial.select {|z| not z.nil? }
           end
           total
       end
   end
end



by ikspres | 2006/06/18 18:19 | ruby | 트랙백 | 덧글(0)
트랙백 주소 : http://ikspres.egloos.com/tb/90501
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

< 이전페이지 다음페이지 >