Sha256: b115be60b96d27e073f6b2fd52f51262e295058f775a456536a70ccacdf2b28e

Contents?: true

Size: 713 Bytes

Versions: 7

Compression:

Stored size: 713 Bytes

Contents

class Array

  unless method_defined?(:permutation) # 1.8.7+

    # 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

    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

  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/array/permutation.rb
facets-2.9.2 src/core/facets/array/permutation.rb
facets-2.9.2 lib/core/facets/array/permutation.rb
facets-2.9.1 lib/core/facets/array/permutation.rb
facets-2.9.0 lib/core/facets/array/permutation.rb
facets-2.9.0.pre.2 lib/core/facets/array/permutation.rb
facets-2.9.0.pre.1 lib/core/facets/array/permutation.rb