Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
19
Task/Entropy/JavaScript/entropy-1.js
Normal file
19
Task/Entropy/JavaScript/entropy-1.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Shannon entropy in bits per symbol.
|
||||
function entropy(str) {
|
||||
const len = str.length
|
||||
|
||||
// Build a frequency map from the string.
|
||||
const frequencies = Array.from(str)
|
||||
.reduce((freq, c) => (freq[c] = (freq[c] || 0) + 1) && freq, {})
|
||||
|
||||
// Sum the frequency of each character.
|
||||
return Object.values(frequencies)
|
||||
.reduce((sum, f) => sum - f/len * Math.log2(f/len), 0)
|
||||
}
|
||||
|
||||
console.log(entropy('1223334444')) // 1.8464393446710154
|
||||
console.log(entropy('0')) // 0
|
||||
console.log(entropy('01')) // 1
|
||||
console.log(entropy('0123')) // 2
|
||||
console.log(entropy('01234567')) // 3
|
||||
console.log(entropy('0123456789abcdef')) // 4
|
||||
15
Task/Entropy/JavaScript/entropy-2.js
Normal file
15
Task/Entropy/JavaScript/entropy-2.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
const entropy = (s) => {
|
||||
const split = s.split('');
|
||||
const counter = {};
|
||||
split.forEach(ch => {
|
||||
if (!counter[ch]) counter[ch] = 1;
|
||||
else counter[ch]++;
|
||||
});
|
||||
|
||||
|
||||
const lengthf = s.length * 1.0;
|
||||
const counts = Object.values(counter);
|
||||
return -1 * counts
|
||||
.map(count => count / lengthf * Math.log2(count / lengthf))
|
||||
.reduce((a, b) => a + b);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue