Sha256: 41df133e2f19fefd93738538d440ffef84974ac3c38755ec11fbac7ae35d3270
Contents?: true
Size: 644 Bytes
Versions: 5
Compression:
Stored size: 644 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
5 entries across 5 versions & 2 rubygems