Last active
March 10, 2017 21:54
-
-
Save animatedlew/2ed9e820b5d56bf3bc2382c490258d98 to your computer and use it in GitHub Desktop.
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
| 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