Skip to content

Instantly share code, notes, and snippets.

@mnikn
Last active June 19, 2019 09:42
Show Gist options
  • Save mnikn/9ac5564cb5dd3c802b5425fc6e63f9b4 to your computer and use it in GitHub Desktop.
Save mnikn/9ac5564cb5dd3c802b5425fc6e63f9b4 to your computer and use it in GitHub Desktop.
[permutation] permutation and combination algorithm #algorithm
function permuate(seq) {
if (seq.length === 0) return [[]];
const solutions = [];
for(let i = 0; i < seq.length; ++i){
const subSolutions = permuate(seq.filter((num, j) => j !== i));
subSolutions.forEach(solution => solutions.push([seq[i]].concat(solution)));
}
return solutions;
}
function subsets(seq) {
if(seq.length === 0) return [[]];
const subsolutions = subsets(seq.slice(1));
return subsolutions.concat(subsolutions.map(solution => [seq[0]].concat(solution)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment