Created
March 20, 2015 19:06
-
-
Save devcem/218888f3e3c13d58e82b to your computer and use it in GitHub Desktop.
High ratio number sequence compress
This file contains 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 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