Skip to content

Instantly share code, notes, and snippets.

@jqr
Created January 12, 2009 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jqr/46111 to your computer and use it in GitHub Desktop.
Save jqr/46111 to your computer and use it in GitHub Desktop.
# http://facets.rubyforge.org/doc/api/core/classes/Array.html#M000145
require 'rubygems'
require 'facets'
class Array
def permutations(min = 0, max = size + 1)
variations = []
(min..max).each do |permutation_size|
permutation(permutation_size) do |permutation|
variations << permutation
end
end
variations
end
end
puts [1, 2, 3].permutations.inspect
# => [[], [1], [2], [3], [1, 2], [2, 1], [1, 3], [3, 1], [2, 3], [3, 2],
# [1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2], [3, 2, 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment