RosettaCodeData/Task/Entropy/Elena/entropy.elena

43 lines
703 B
Plaintext

import system'math;
import system'collections;
import system'routines;
import extensions;
extension op
{
logTwo()
= self.ln() / 2.ln();
}
public program()
{
var input := console.readLine();
var infoC := 0.0r;
var table := new Dictionary();
input.forEach:(ch)
{
var n := table[ch];
if (nil == n)
{
table[ch] := 1
}
else
{
table[ch] := n + 1
}
};
var freq := 0;
table.forEach:(letter)
{
freq := letter.toInt().realDiv(input.Length);
infoC += (freq * freq.logTwo())
};
infoC *= -1;
console.printLine("The Entropy of ", input, " is ", infoC)
}