Skip to content

Instantly share code, notes, and snippets.

@TarasMartynyuk
Last active April 26, 2018 21:09
Show Gist options
  • Save TarasMartynyuk/2e99fa4eea2e9d5f2f8f7b26d6f2274f to your computer and use it in GitHub Desktop.
Save TarasMartynyuk/2e99fa4eea2e9d5f2f8f7b26d6f2274f to your computer and use it in GitHub Desktop.
function vb(n) {
var binary = n.toString(2);
var expectedLength = Math.ceil(binary.length / 7) * 7; // works
var sevenByteBlocks = binary.padStart(expectedLength, '0').match(/\d{7}/g);
var lastElemIdx = sevenByteBlocks.length - 1;
for (var i = 0; i < lastElemIdx; i++) {
sevenByteBlocks[i] = '0' + sevenByteBlocks[i];
}
sevenByteBlocks[lastElemIdx] = '1' + sevenByteBlocks[lastElemIdx];
return sevenByteBlocks.join('');
}
function unary(n) {
return '0'.padStart(n + 1, '1');
}
function gamma(n) {
var bin = n.toString(2);
return unary(bin.length - 1) + bin.substring(1, );
}
function fMeasure(p, r) { return 2 * p*r/(p + r); }
function prec(tp, fp) { return tp / (tp + fp); /* fp is what we did return but musn't have */ }
function rec(tp, fn) { return tp / (tp + fn); /* fn is what we must have returned but didn't */ }
function ct(r_cont, n_cont, r_mis, n_mis, base=10) {
var log_func = getLogFunc(base);
if(log_func === null) {return;}
// N = r_cont + n_cont + r_mis + n_mis; // 2 for 1/2s later
// DFt = (r_cont + n_cont) / N;
// S_s = r_mis;
// s = r_cont;
return log_func ( (r_cont / r_mis) / (n_cont / n_mis) );
}
function getLogFunc(base) {
switch(base) {
case 2:
console.log("using log2");
return Math.log2;
case 10:
console.log("using log10");
return Math.log10;
case "n":
console.log("using log");
return Math.log;
default:
console.log("invalid base");
return null;
}
}
function soundex(word) {
encoded = "";
for (let i = 1; i < word.length; i++) {
const element = word[i];
encoded += encode(word[i]);
}
console.log('encoded is ' + encoded);
res = word[0];
curr_char = "~";
for(let i = 0; i < encoded.length - 1; i++) {
if(encoded[i] != curr_char) {
curr_char = encoded[i];
res += encoded[i];
}
}
if(res.length >= 4) {
return res.substring(0, 4);
} else {
diff = 4 - res.length;
return res.padEnd(diff, "0")
}
}
function encode(ch){
if("a e i o u y h w".indexOf(ch) > -1) {
return 0;
}
if("b, f, p, v".indexOf(ch) > -1) {
return 1;
}
if("c, g, j, k, q, s, x, z ".indexOf(ch) > -1) {
return 2;
}
if("d, t".indexOf(ch) > -1) {
return 3;
}
if('l'.indexOf(ch) > -1) {
return 4;
}
if('m, n'.indexOf(ch) > -1) {
return 5;
}
if('r'.indexOf(ch) > -1) {
return 6;
}
console.log('wrong char');
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment