Skip to content

Instantly share code, notes, and snippets.

@sagivo

sagivo/Permutations

Last active Aug 29, 2015
Embed
What would you like to do?
permutation in ruby
def perm arr, i=0
return p arr if i == arr.size
(i..arr.size-1).each do |j|
arr[i], arr[j] = arr[j], arr[i]
perm arr, i+1
arr[i], arr[j] = arr[j], arr[i]
end
end
perm 'ABC'
-------------
"ABC"
"ACB"
"BAC"
"BCA"
"CBA"
"CAB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.