Skip to content

Instantly share code, notes, and snippets.

@airhorns
Created August 23, 2011 16:06
Show Gist options
  • Save airhorns/1165647 to your computer and use it in GitHub Desktop.
Save airhorns/1165647 to your computer and use it in GitHub Desktop.
coffeescript permutations
permute = (array) ->
buf = []
return array if array.length == 1
for element, i in array
buf.push [element]
remaining = array.slice()
remaining.splice(i, 1)
subPermutations = permute(remaining)
buf.concat subPermutations
for sub in subPermutations
buf.push [element].concat(sub)
buf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment