- #1
Jamin2112
- 986
- 12
Okay, so I have a function single_ops(w) that returns a JavaScript array of all strings that can be made from 1 operation on the string w:
Yes, I know the procedure is messy and crappy. But it works for now. My problem is something different.
I want a procedure that uses that recursively calls that function so that I can have a set of all strings made from n operations on w. In other words, call the function once, returning an array A of all strings that are formed from 1 operation on w then, on each element of A, call the function again and merge the elements of the returned array to A, etc.
I'm working on it but having trouble with the part where I have a variable that is limiting the number of recursions. Any help?
Code:
function single_ops(w)
{
/* Given a word w, return an array of all strings that are w with:
(1) 1 letter inserted, that letter being one which is distance
1 away on the keyboard
(2) 1 letter replaced, that letter being one which is distance
1 away on the keyboard
(3) 1 letter removed
(4) 2 adjacent letters swapped
*/
var A = new Array(); // Associative array to store words, so we can add them w/out addding repeats
for (var i = 0, j = w.length; i < j; ++i)
{
var closeChars = kdmap[w[i]];
var s1a = w.slice(0,i); // equals w[0], ..., w[i-1]
var s2a = w.slice(i,j); // equals w[i], ..., w[j-1]
var s1b = w.slice(0,i+1); // equals w[0], ..., w[i]
var s2b = w.slice(i+1,j) // equals w[i+1], ..., w[j-1]
for (var k = 0, n = closeChars.length; k < n; ++k)
{
var cc = closeChars[k];
// (1) ----------------------------------------------
A[s1a + cc + s2a] = 1; // equals w[0],..., w[i-1], cc, w[i],..., w[j-1]
A[s1b + cc + s2b] = 1; // equals w[0], ..., w[i], cc, w[i+1], ..., w[j-1]
// (2) ----------------------------------------------
A[s1a + cc + s2b] = 1; // equals w[0], ..., w[i-1], cc, w[i+1], ..., w[j-1]
}
// (3) --------------------------------------------------
if (j > 1)
A[s1a + s2b] = 1; // equals w[0], ..., w[i-1], w[i+1], ..., w[j-1]
// (4) --------------------------------------------------
A[s1a + s2b.slice(0,1) + s2a.slice(0,1) + s2b.slice(1,s2b.length)] = 1;
}
return A;
}
Yes, I know the procedure is messy and crappy. But it works for now. My problem is something different.
I want a procedure that uses that recursively calls that function so that I can have a set of all strings made from n operations on w. In other words, call the function once, returning an array A of all strings that are formed from 1 operation on w then, on each element of A, call the function again and merge the elements of the returned array to A, etc.
I'm working on it but having trouble with the part where I have a variable that is limiting the number of recursions. Any help?