SHARE
TWEET

Untitled

a guest Oct 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // запускать программу надо так: node Entropy.js abrakadabra
  2.  
  3. let arg = process.argv;
  4.  
  5. input = arg[2]; // входная строка
  6. letters = new Array(); // тута будут храниться кол-ва всех встречающихся букв
  7. let lenghtLetters = 0; // длина массива letters
  8.  
  9. for (let i = 0; i < input.length; i -= -1)
  10. {
  11.     if (letters[input.charAt(i)])
  12.         letters[input.charAt(i)]++;
  13.     else
  14.     {
  15.         letters[input.charAt(i)] = 1;
  16.         lenghtLetters++;
  17.     }
  18. }
  19.  
  20. let entropy = 0;
  21. if (lenghtLetters > 0) // просто реализация формулы Энтропия = -1 * (sum_(k от 1 до lenghtLetters) [p{i} * log(p{i}) / log(lenghtLetters)] )
  22. {
  23.     for (let element in letters)
  24.     {  
  25.         entropy -= letters[element] * Math.log(letters[element]);
  26.         entropy /= Math.log(lenghtLetters);
  27.     }
  28. }
  29. console.log(entropy);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top