Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Entropy/Jsish/entropy.jsish
Normal file
30
Task/Entropy/Jsish/entropy.jsish
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* Shannon entropy, in Jsish */
|
||||
|
||||
function values(obj:object):array {
|
||||
var vals = [];
|
||||
for (var key in obj) vals.push(obj[key]);
|
||||
return vals;
|
||||
}
|
||||
|
||||
function entropy(s) {
|
||||
var split = s.split('');
|
||||
var counter = {};
|
||||
split.forEach(function(ch) {
|
||||
if (!counter[ch]) counter[ch] = 1;
|
||||
else counter[ch]++;
|
||||
});
|
||||
|
||||
var lengthf = s.length * 1.0;
|
||||
var counts = values(counter);
|
||||
return -1 * counts.map(function(count) {
|
||||
return count / lengthf * (Math.log(count / lengthf) / Math.log(2));
|
||||
})
|
||||
.reduce(function(a, b) { return a + b; }
|
||||
);
|
||||
};
|
||||
|
||||
if (Interp.conf('unitTest')) {
|
||||
; entropy('1223334444');
|
||||
; entropy('Rosetta Code');
|
||||
; entropy('password');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue