Last active
February 20, 2017 14:15
-
-
Save sushiljainam/71b8f5ad2816102797479a5e71d93e4c to your computer and use it in GitHub Desktop.
to print all combinations for states, given length
This file contains hidden or 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
function comb(len, states, rules){ | |
if(len>16){ | |
// return "out of limit, put force if you know better"; | |
} | |
var states = states || ["0","1"]; | |
var pos = []; | |
var powers = []; | |
for (var i = 0; i < len; i++) { | |
pos.push(0); | |
}; | |
var rule = rules && rules[0]; | |
var ruleVal = 0; | |
for (var i = 0; i < states.length; i++) { | |
powers.push(Math.pow(len, i)); | |
if (!!rule) { | |
ruleVal += rule[i]*powers[i]; | |
} else { | |
ruleVal = rule; | |
} | |
}; | |
var shouldBreak, str; | |
var output = []; | |
do{ | |
shouldBreak = true; | |
str = "";var s=0; | |
for (var i = 0; i < len; i++) { | |
str+= states[pos[i]]; | |
s+= powers[pos[i]]; | |
}; | |
// console.log('output',s, s==ruleVal, !ruleVal, str); | |
// console.log('output', str); | |
(!ruleVal || s==ruleVal) && output.push(str); | |
for (var i = len-1; i >= 0; i--) { | |
if(pos[i]<states.length-1){ | |
pos[i]++; break; | |
} | |
if(pos[i]<states.length){ | |
if (i==0) { | |
shouldBreak = false; break; | |
}; | |
pos[i]=0; | |
} | |
}; | |
}while(shouldBreak); | |
return output; | |
} | |
// comb(2) | |
// comb(3) | |
console.time('s'); | |
// var output = comb(6,['s','u','s','h','i','l'],[[1,1,1,1,1,1]]); | |
// var output = comb(6,['s','u','h','i','l']); | |
// var output = comb(3); | |
// var output = comb(6, ['l','n','o','r','u'],[[1,1,2,1,1]]); | |
var output = listCombName('meel'); | |
console.log('output', JSON.stringify(output)); | |
console.log('count = ', output.length); | |
console.timeEnd('s'); | |
// console.time('s2'); | |
// var output = comb(5,['a','a','a','b','c']); | |
// // console.log('output', output); | |
// console.log('count = ', output.length); | |
// console.timeEnd('s2'); | |
// | |
// comb(18): 1241 ms, console inside | |
// comb(19): 2437 ms, console inside | |
// comb(20): 5220 ms, console inside | |
// comb(21): 13026 ms, console inside :count = 2097152 | |
// comb(22): 23742 ms, console inside :count = 4194304 | |
//// with --max_old_space_size=2000000 | |
//// with --max_old_space_size=2000 :FATAL_ERROR | |
//// with --max_old_space_size=2500 :time = 32109 ms | |
function listCombName (s) { | |
var sortedChars = []; | |
sortedChars[0] = s[0]; | |
var rules = []; | |
var freq = rules[0] = []; | |
for (var i = 1,k=1; i < s.length; i++) { | |
if(charInArray(s[i], sortedChars)) {continue;} | |
sortedChars[k++] = s[i]; | |
for (var j = i; j > 0; j--) { | |
if (sortedChars[j-1]>sortedChars[j]) { | |
var t = sortedChars[j]; | |
sortedChars[j] = sortedChars[j-1]; | |
sortedChars[j-1] = t; | |
}; | |
} | |
// console.log(sortedChars); | |
}; | |
for (var i = 0; i < sortedChars.length; i++) { | |
freq[i] = freqInString(sortedChars[i], s); | |
}; | |
function freqInString (c, s) { | |
var r = 0; | |
for (var i = 0; i < s.length; i++) { | |
s[i] == c && r++; | |
}; | |
return r; | |
} | |
function charInArray (c, arr) { | |
for (var i = 0; i < arr.length; i++) { | |
if(c==arr[i]){ | |
return true; | |
} | |
}; | |
} | |
// console.log(sortedChars, rules); | |
return comb(s.length, sortedChars, rules); | |
} | |
// listCombName('meenal') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bug removed by major change #1