Skip to content

Instantly share code, notes, and snippets.

@bsboris
Created October 19, 2021 19:35
Show Gist options
  • Select an option

  • Save bsboris/34a7cc8ef4b66dc62480c27f16a01205 to your computer and use it in GitHub Desktop.

Select an option

Save bsboris/34a7cc8ef4b66dc62480c27f16a01205 to your computer and use it in GitHub Desktop.
Array permutations
def pm(n)
return [] if n == 0
return [[0]] if n == 1
[].tap do |out|
pm(n-1).each do |sub|
n.times do |i|
out << sub.dup.insert(i-=1, n-1)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment