Strings
Data Types
function swap(arr, a, b) { let tmp = arr[a]; arr[a] = arr[b]; arr[b] = tmp; return arr; } function getNextShuffle(arr) { let n = arr.length; let j = n - 2; let l = n - 1; while (arr[j] >= arr[j + 1] && j > -1) { j--; } if (j === -1) { return false } while (arr[j] >= arr[l] && j < l) { l-- } arr = swap(arr, j, l); arr = arr.slice(0, j + 1).concat(arr.slice(j + 1).reverse()); return arr; } function allAnagrams(word, noRepeats) { let shuffle = word.split('').map((item, i) => i); let result = []; do { let anagram = shuffle.map((index) => word[index]).join(''); if (!noRepeats || result.indexOf(anagram) < 0) { result.push(anagram); } shuffle = getNextShuffle(shuffle); } while (!!shuffle); return result }
function allAnagrams(word) {- function swap(arr, a, b) {
- let tmp = arr[a];
- arr[a] = arr[b];
- arr[b] = tmp;
- return arr;
- }
if (word.length < 2) {return [word];} else {var allAnswers = [];for(var i = 0; i < word.length; i++) {var letter = word[i];var shorterWord = word.substr(0, i) + word.substr(i+1, word.length - 1);var shortWordArray = allAnagrams(shorterWord);for (var j = 0; j < shortWordArray.length; j++) {allAnswers.push(letter + shortWordArray[j]);}}return allAnswers;}- function getNextShuffle(arr) {
- let n = arr.length;
- let j = n - 2;
- let l = n - 1;
- while (arr[j] >= arr[j + 1] && j > -1) {
- j--;
- }
- if (j === -1) {
- return false
- }
- while (arr[j] >= arr[l] && j < l) {
- l--
- }
- arr = swap(arr, j, l);
- arr = arr.slice(0, j + 1).concat(arr.slice(j + 1).reverse());
- return arr;
- }
- function allAnagrams(word, noRepeats) {
- let shuffle = word.split('').map((item, i) => i);
- let result = [];
- do {
- let anagram = shuffle.map((index) => word[index]).join('');
- if (!noRepeats || result.indexOf(anagram) < 0) {
- result.push(anagram);
- }
- shuffle = getNextShuffle(shuffle);
- } while (!!shuffle);
- return result
- }
// With anagram repeats Test.assertSimilar(allAnagrams("bu"), ["bu", "ub"] ); Test.assertSimilar(allAnagrams("lol"), ["lol", "llo", "oll", "oll", "llo", "lol"] ); Test.assertSimilar(allAnagrams("dog"), ["dog", "dgo", "odg", "ogd", "gdo", "god"] ); Test.assertSimilar(allAnagrams("four"), ["four", "foru", "fuor", "furo", "frou", "fruo", "ofur", "ofru", "oufr", "ourf", "orfu", "oruf", "ufor", "ufro", "uofr", "uorf", "urfo", "urof", "rfou", "rfuo", "rofu", "rouf", "rufo", "ruof"] ); Test.assertSimilar(allAnagrams("lazy"), ["lazy", "layz", "lzay", "lzya", "lyaz", "lyza", "alzy", "alyz", "azly", "azyl", "aylz", "ayzl", "zlay", "zlya", "zaly", "zayl", "zyla", "zyal", "ylaz", "ylza", "yalz", "yazl", "yzla", "yzal"] ); // Without repeats Test.assertSimilar(allAnagrams("bu", true), ["bu", "ub"] ); Test.assertSimilar(allAnagrams("lol", true), ["lol", "llo", "oll"]); Test.assertSimilar(allAnagrams("dog", true), ["dog", "dgo", "odg", "ogd", "gdo", "god"] ); Test.assertSimilar(allAnagrams("aaa", true), ["aaa"] );
// TODO: Replace examples and use TDD development by writing your own tests- // With anagram repeats
- Test.assertSimilar(allAnagrams("bu"), ["bu", "ub"] );
- Test.assertSimilar(allAnagrams("lol"), ["lol", "llo", "oll", "oll", "llo", "lol"] );
- Test.assertSimilar(allAnagrams("dog"), ["dog", "dgo", "odg", "ogd", "gdo", "god"] );
- Test.assertSimilar(allAnagrams("four"), ["four", "foru", "fuor", "furo", "frou", "fruo", "ofur", "ofru", "oufr", "ourf", "orfu", "oruf", "ufor", "ufro", "uofr", "uorf", "urfo", "urof", "rfou", "rfuo", "rofu", "rouf", "rufo", "ruof"] );
- Test.assertSimilar(allAnagrams("lazy"), ["lazy", "layz", "lzay", "lzya", "lyaz", "lyza", "alzy", "alyz", "azly", "azyl", "aylz", "ayzl", "zlay", "zlya", "zaly", "zayl", "zyla", "zyal", "ylaz", "ylza", "yalz", "yazl", "yzla", "yzal"] );
// These are some CW specific test methods available:// Test.expect(boolean, [optional] message)// Test.assertEquals(actual, expected, [optional] message)// Test.assertSimilar(actual, expected, [optional] message)// Test.assertNotEquals(actual, expected, [optional] message)// NodeJS assert is also automatically required for you.// assert(true)// assert.strictEqual({a: 1}, {a: 1})// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})// You can also use Chai (http://chaijs.com/) by requiring it yourself// var expect = require("chai").expect;// var assert = require("chai").assert;// require("chai").should();Test.expect(allAnagrams("bu"), ["bu", "ub"] );Test.expect(allAnagrams("lol"), ["lol", "llo", "oll", "oll", "llo", "lol"] );Test.expect(allAnagrams("dog"), ["dog", "dgo", "odg", "ogd", "gdo", "god"] );Test.expect(allAnagrams("four"), ["four", "foru", "fuor", "furo", "frou", "fruo", "ofur", "ofru", "oufr", "ourf", "orfu", "oruf", "ufor", "ufro", "uofr", "uorf", "urfo", "urof", "rfou", "rfuo", "rofu", "rouf", "rufo", "ruof"] );Test.expect(allAnagrams("lazy"), ["lazy", "layz", "lzay", "lzya", "lyaz", "lyza", "alzy", "alyz", "azly", "azyl", "aylz", "ayzl", "zlay", "zlya", "zaly", "zayl", "zyla", "zyal", "ylaz", "ylza", "yalz", "yazl", "yzla", "yzal"] )- // Without repeats
- Test.assertSimilar(allAnagrams("bu", true), ["bu", "ub"] );
- Test.assertSimilar(allAnagrams("lol", true), ["lol", "llo", "oll"]);
- Test.assertSimilar(allAnagrams("dog", true), ["dog", "dgo", "odg", "ogd", "gdo", "god"] );
- Test.assertSimilar(allAnagrams("aaa", true), ["aaa"] );