Created
April 28, 2020 15:03
-
-
Save gkucmierz/d187632830d71b31bf5f101e46ae329a to your computer and use it in GitHub Desktop.
Generate Permutations
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
const genPermutations = size => { | |
const perms = []; | |
const gen = (res, used, shift) => { | |
if (used === size) { | |
perms.push(res); | |
} | |
for (let i = 0; i < (size-used); ++i) { | |
gen([...res, shift[i]], used + 1, [...shift.slice(0, i), ...shift.slice(i + 1)]); | |
} | |
}; | |
gen([], 0, new Array(size).fill(0).map((_, i) => i)); | |
return perms; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment