Created
April 16, 2016 11:42
-
-
Save bigomega/8164e9da5bf03b6916f2040e05bc8dd5 to your computer and use it in GitHub Desktop.
Coding problem
This file contains 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
dict = ['cat','cot','cog','bog','bat','bap','map', 'mat']; | |
start = 'cat'; | |
end = 'map'; | |
isLinked = function(w1, w2){ | |
var flag = false; | |
for (var i = 0; i < w1.length; i++) { | |
if(w1[i] != w2[i]){ | |
if(flag) | |
return false; | |
flag = true; | |
} | |
} | |
return true; | |
} | |
myFunc = function(status, dict_copy) { | |
var out = []; | |
for (var i = dict_copy.length - 1; i >= 0; i--) { | |
var dictCopy = dict_copy.slice(0); | |
var st = status.slice(0); | |
var next = dictCopy[i]; | |
dictCopy.splice(i, 1); | |
if(isLinked(st[st.length - 1], next)){ | |
st.push(next); | |
if(next == end){ | |
out.push(st); | |
} else { | |
var zzz = myFunc(st, dictCopy, out); | |
out = out.concat(zzz); | |
} | |
} | |
} | |
return out; | |
} | |
if(start == end){ | |
x = [start]; | |
} else { | |
var dictCopy = dict.filter(function(val){ return val != start;}); | |
x = myFunc([start], dictCopy); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment