Skip to content

Instantly share code, notes, and snippets.

@ex
Created January 15, 2011 23:31
Show Gist options
  • Select an option

  • Save ex/781372 to your computer and use it in GitHub Desktop.

Select an option

Save ex/781372 to your computer and use it in GitHub Desktop.
permutation
def permutations(array)
n = array.length
return [array] if n < 2
rt = Array.new
for i in 0...n
## Create a new array from [array] where the
## [i]-th term has been ignored.
t = Array.new(array)
t.delete_at(i)
tz = permutations(t)
tz.each do |tp|
rt << tp.unshift(array[i])
end
end
return rt
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment