Created
November 22, 2017 10:21
-
-
Save rickdaalhuizen90/90538c3c414a4479668ff890a225fc50 to your computer and use it in GitHub Desktop.
Find all the possible permutations using Ruby and recursion Ask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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