| Class | Permutations |
| In: |
permutations.rb
|
| Parent: | Object |
yield a sequence of all the permutations of numbers 0 .. n-1. returns nil
# File permutations.rb, line 30 def each return nil if @n <= 0 ret = @n.factorial return ret if not block_given? permute(@n) {|a| yield a} ret end
yield a sequence of all the permuations of numbers 0 .. n-1. The algorithm is:
returns nil.
# File permutations.rb, line 45 def permute(n) if n == 1 yield [0] return end permute(n - 1) do |a| b = a.clone << (n - 1) n.times { |i| yield b.rotate! } end end