-
-
Save williscool/2cadbde4a0386db81271 to your computer and use it in GitHub Desktop.
Function for generating permutations of a list.
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
// permutations(["c","a","t"]) | |
function permutations(array){ | |
if (array.length === 0) return [[]]; | |
var perms = []; | |
for(var i = 0; i < array.length; i++) { | |
var copy = array.slice(0); | |
var prefix = copy.splice(i,1); | |
var rest = permutations(copy); | |
for(var j = 0; j < rest.length ; j++) { | |
perms.push(prefix.concat(rest[j])); | |
} | |
} | |
return perms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment