Showing posts with label search suggestions. Show all posts
Showing posts with label search suggestions. Show all posts

Saturday, March 20, 2010

Solution to search suggestions with permutition

Hi All,

In one of requirement i need to show the all possible combinitation of text provided in input to show possible search suggestions, i started thinking like school student of permutition and i found the solution, here is my code i have solved with Javascript

permute = function(v, m){ //v1.0
for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
for(r = new Array(q); ++p < q;)
for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
for(x[3][i] = x[2][i], f = 0; !f; f = !f)
for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
return r;
};

function callPermute()
{
var a = ["A","B","C"], j = permute(a);
document.write("< h2 >", a.join(" "), " = ", j.length, "< /h2 >",j.join("< br / >"));
}

you can call the method callPermute() to show the require result

Happy Coding :)