Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Last active March 10, 2017 21:54
Show Gist options
  • Select an option

  • Save animatedlew/2ed9e820b5d56bf3bc2382c490258d98 to your computer and use it in GitHub Desktop.

Select an option

Save animatedlew/2ed9e820b5d56bf3bc2382c490258d98 to your computer and use it in GitHub Desktop.
function permute(xs) {
function p(xs, prefix) {
if (!xs.length) return prefix;
else {
let results = [];
for (let i = 0; i < xs.length; i++) {
let rem = xs.slice(0, i) + xs.slice(i+1);
results = results.concat(p(rem, prefix + xs[i]));
}
return results;
}
}
return p(xs, "");
}
let isAnagram = (_) => Math.round(Math.random());
let anagrams = ["abc", "car", "bus"]
.map(permute)
.reduce((b, a) => isAnagram(a) ? b.concat(a) : b, []);
console.log(anagrams);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment