Sha256: 435e473d0ad223225e2a68fb69121ad7f323a1b5c4a3b53226ba162d17a60f91

Contents?: true

Size: 643 Bytes

Versions: 4

Compression:

Stored size: 643 Bytes

Contents

unless (RUBY_VERSION[0,3] == '1.9')

  class Array

    # 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.
    #
    # 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

4 entries across 4 versions & 1 rubygems

Version Path
facets-2.6.0 lib/core/facets/array/permutation.rb
facets-2.5.0 lib/core/facets/array/permutation.rb
facets-2.5.1 lib/core/facets/array/permutation.rb
facets-2.5.2 lib/core/facets/array/permutation.rb