Skip to content

Instantly share code, notes, and snippets.

@rickdaalhuizen90
Created November 22, 2017 10:21
Show Gist options
  • Save rickdaalhuizen90/90538c3c414a4479668ff890a225fc50 to your computer and use it in GitHub Desktop.
Save rickdaalhuizen90/90538c3c414a4479668ff890a225fc50 to your computer and use it in GitHub Desktop.
Find all the possible permutations using Ruby and recursion Ask
# https://stackoverflow.com/questions/25224321/find-all-the-possible-permutations-using-ruby-and-recursion
$letters = "prtsone"
# Make permutations from letters
def permutation(string)
return [string] if string.size < 2
ch = string[0]
permutation(string[1..-1]).each_with_object([]) do |perm, result|
(0..perm.size).each { |i| result << perm.dup.insert(i,ch) }
end
end
$permutations = permutation($letters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment