17 lines
312 B
Ruby
17 lines
312 B
Ruby
#!/usr/bin/env ruby
|
|
|
|
class Array
|
|
def combinations n
|
|
result = Array.new
|
|
if n > 0
|
|
(0 .. self.length - n).each do |i|
|
|
combs = self[i + 1 .. -1].combinations(n - 1)
|
|
combs = [[]] if combs.empty?
|
|
combs.collect {|comb| [self[i]] + comb}.each {|x| result << x}
|
|
end
|
|
end
|
|
return result
|
|
end
|
|
end
|
|
|