Sha256: bd5dd5a7995521cc3d35eddb9c24c21edd9551082bf3447d38b9f3b483e4dba3
Contents?: true
Size: 653 Bytes
Versions: 6
Compression:
Stored size: 653 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. # # 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
6 entries across 6 versions & 1 rubygems