Last active
June 19, 2019 09:42
-
-
Save mnikn/9ac5564cb5dd3c802b5425fc6e63f9b4 to your computer and use it in GitHub Desktop.
[permutation] permutation and combination algorithm #algorithm
This file contains hidden or 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
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