Skip to content

Instantly share code, notes, and snippets.

@bbars
Created March 17, 2023 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbars/2e2561d96c542047ff9377c81f2b9e44 to your computer and use it in GitHub Desktop.
Save bbars/2e2561d96c542047ff9377c81f2b9e44 to your computer and use it in GitHub Desktop.
Generate all combinations of provided element set
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