javascript - all anagrams - recursion -
i'm trying understand following recursive function below teach myself recursion. attempt explain below expected output , function itself. missing? i'm not seeing process 'abc' 'acb'. attempt understand got me 'abc' right 'bac'.
example usage: var anagrams = allanagrams('abc'); console.log(anagrams); // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ] var allanagrams = function(string) { var uniqueoutput = {}; (function anagram(ana, str) { // have written as: if(!str).... if (str === '') { uniqueoutput[ana] = 1; } //recursive call length of anagram. (var = 0; < str.length; i++) { anagram(ana + str[i], str.slice(0, i) + str.slice(i + 1)); console.log(ana); } })('', string); console.log(uniqueoutput) return object.keys(uniqueoutput); }; // calling recursive function this: anagram([anagram=]'', [string=]'abc') // base case not met on first iteration since string isn't empty // first iteration: = 0 // anagram('' + 'a' + 'bc' [from str.slice(0 +1)]) ---> resolves "abc") ---> anagram("abc") //second iteration: = 1. still in original call line 37. // here below, "ana" stay '' since still inside initial recursion call? //anagram('' + b + + c) ---- resolves "bac" ---> anagram("bac") //third , last iteration iteration: = 2 //anagram(" " + c + c) ----> anagram("cc") ? not valid result. my new, correct (i think) explanation:
//initial input: anagram("", "abc") //step 1: enter function --> = 0 original string "abc" //anagram("" + "a", "bc") ----> anagram("a", "bc") //step 2: loop through new, modified string, "bc" //var = 0; //anagram("a" + "b", "c")---> anagram("ab", "c") //anagram("ab" + "c", [nothing here]) //base case hit, uniqueoutput["abc"] = 1; //var = 1; --> applies string "bc". loop gets reset = 0 once have new string worth (like below, "b") //anagram("a" + "c", "b") //anagram("ac", "b") //anagram("ac" + "b", "" ) //base case hit, uniqueoutput["acb"] = 1; //step 3: increment on original string input ("abc") --> dealing str[i] === b //var = 1; //anagram("" + "b", "a" + "c") //anagram("b", "ac") ---> need loop through "ac"! //anagram("b" + "a", "c") //anagram("ba", "c") //anagram("bac", "")---> base case hit, uniqueoutput["bac"] = 1; //anagram("b", "ac") //anagram("b" + "c", "a") ---> anagram("bc", "a") //anagram("bca", "") ---> base case hit, uniqueoutput["bca"] = 1; //step 4: increment on original string input ("abc") ---> str[i] === c //var = 2; //anagram ("" + "c", "ab")---> anagram("c", "ab") //now need loop through "ab!" c's index stays same. //anagram("c" + "a", "b") ---> anagram("ca", "b") //anagram("cab", '')---> uniqueouput["cab"] = 1; //anagram("c" + "b", "a") ---> anagram("cb", "a") //anagram("cba", "")----> uniqueoutput["cba"] = 1
from comments above:
// first iteration: = 0 // anagram('' + 'a' + 'bc' [from str.slice(0 +1)]) ---> resolves "abc") ---> actually on i = 0, arguments passed anagram are:
anagram('' + 'a', '' + 'bc'); which evaluates to:
anagram('a', 'bc'); then within call anagram, again loop on str, 'bc'. result in 2 more calls anagram be
anagram('a' + 'b', '' + 'c'); // = 0 anagram('a' + 'c', 'b' + ''); // = 1 which evaluate to:
anagram('ab', 'c'); anagram('ac', 'b'); and second 1 of calls result in call anagram these arguments:
anagram('acb', ''); which gets added uniqueoutput str empty.
only after has executed code return outermost call of anagram , i increment per comment:
//second iteration: = 1. still in original call line 37.
Comments
Post a Comment