Created
March 17, 2023 14:57
-
-
Save bbars/2e2561d96c542047ff9377c81f2b9e44 to your computer and use it in GitHub Desktop.
Generate all combinations of provided element set
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
function* permute(permutation) { | |
let length = permutation.length; | |
let c = Array(length).fill(0); | |
let i = 1; | |
let k; | |
let p; | |
yield permutation; | |
while (i < length) { | |
if (c[i] < i) { | |
k = i % 2 && c[i]; | |
p = permutation[i]; | |
permutation[i] = permutation[k]; | |
permutation[k] = p; | |
++c[i]; | |
i = 1; | |
yield permutation; | |
} | |
else { | |
c[i] = 0; | |
++i; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment