Method: Array#permutation

Defined in:
lib/core/facets/array/permutation.rb

#permutation(n = size) ⇒ Object

Permutation provids the possible orders of an enumerable. Each is indexed by a permutation number. The maximum number of arrangements is the factorial of the size of the array.

[1,2].permutation(2).to_a #=> [[1,2], [2,1]]

CREDIT: Shin-ichiro Hara



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/core/facets/array/permutation.rb', line 13

def permutation(n=size)
  if size < n or n < 0
  elsif n == 0
    yield([])
  else
    self[1..-1].permutation(n - 1) do |x|
      (0...n).each do |i|
        yield(x[0...i] + [first] + x[i..-1])
      end
    end
    self[1..-1].permutation(n) do |x|
      yield(x)
    end
  end
end