Skip to content

Instantly share code, notes, and snippets.

@devcem
Created March 20, 2015 19:06
Show Gist options
  • Save devcem/218888f3e3c13d58e82b to your computer and use it in GitHub Desktop.
Save devcem/218888f3e3c13d58e82b to your computer and use it in GitHub Desktop.
High ratio number sequence compress
function pad(num,size) {
var s = "000000000" + num;
return s.substr(s.length-size);
}
var bitLength = 2; //Size of each number
var headType = 8; //Min integer in bytes will write in head for index.
function bit(val){
var min = Math.min.apply(Math, val);
var head = parseInt(headType/min*4);
var output = pad((head).toString(2),3);
for(x=0;x<val.length;x++){
output += pad((val[x] - min).toString(2),bitLength);
}
return output;
}
function solve(val,length){
var res = headType/parseInt(val.substr(0, 3),2)*4;
var num = [];
for(i=0;i<length;i++){
num[i] = res+parseInt(val.substr(3+i*bitLength, bitLength),2);
}
return num;
}
console.log(bit([11, 12, 11, 10, 8, 10, 9, 10, 10]));
console.log(solve(bit([11, 12, 11, 10, 8, 10, 9, 10, 10]),9));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment