Pistos (owner)

Forks

Revisions

gist: 96425 Download_button fork
public
Description:
Off-the-top-of-my-head implementation of array permutation.
Public Clone URL: git://gist.github.com/96425.git
Embed All Files: show embed
permutations.rb #
1
2
3
4
5
6
7
8
9
10
class Array
  def perms
    return [ self ] if size < 2
    map { |e|
      ( self - [ e ] ).perms.map { |p| [ e ] + p }
    }.flatten( 1 )
  end
end
 
p [ 'a', 'b', 'f', [ 1,2 ] ].perms