RosettaCodeData/Task/Entropy/Elena/entropy.elena

43 lines
703 B
Text
Raw Permalink Normal View History

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