Skip to content

Instantly share code, notes, and snippets.

@Toshakins
Created December 13, 2013 00:33
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 Toshakins/7938159 to your computer and use it in GitHub Desktop.
Save Toshakins/7938159 to your computer and use it in GitHub Desktop.
ruby acrobatics
#recursive lexicographic permutations
def perm(str)
res = []
if str.length <= 1
return res << str
else
t = []
str.each_char do |chr|
t += perm(str.delete(chr))
t.each do |permutation|
res << chr + permutation unless permutation.include?(chr)
end
end
end
return res
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment