Skip to content

Instantly share code, notes, and snippets.

@bobbzorzen
Created April 30, 2020 16:25
Show Gist options
  • Save bobbzorzen/933e048b96d342fd0bb132a943f01a10 to your computer and use it in GitHub Desktop.
Save bobbzorzen/933e048b96d342fd0bb132a943f01a10 to your computer and use it in GitHub Desktop.
Word permutations
const originalWords = "makes my dick hard";
const wordArray = originalWords.split(" ");
function perm(xs) {
let ret = [];
for (let i = 0; i < xs.length; i = i + 1) {
let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1)));
if (!rest.length) {
ret.push([xs[i]])
} else {
for (let j = 0; j < rest.length; j = j + 1) {
ret.push([xs[i]].concat(rest[j]))
}
}
}
return ret;
}
const permutations = perm(wordArray);
const mergedPermutations = permutations.map(item=> item.join(" "));
console.log("permutations: ");
console.log(mergedPermutations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment