RosettaCodeData/Task/Entropy/Elena/entropy.elena

43 lines
705 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import system'math;
import system'collections;
import system'routines;
import extensions;
extension op
{
logTwo()
= self.ln() / 2.ln();
}
2026-02-01 16:33:20 -08:00
public Program()
2023-07-01 11:58:00 -04:00
{
var input := console.readLine();
var infoC := 0.0r;
var table := Dictionary.new();
2024-03-06 22:25:12 -08:00
input.forEach::(ch)
2023-07-01 11:58:00 -04:00
{
var n := table[ch];
if (nil == n)
{
table[ch] := 1
}
else
{
table[ch] := n + 1
}
};
var freq := 0;
2024-03-06 22:25:12 -08:00
table.forEach::(letter)
2023-07-01 11:58:00 -04:00
{
freq := letter.toInt().realDiv(input.Length);
infoC += (freq * freq.logTwo())
};
infoC *= -1;
2025-08-11 18:05:26 -07:00
Console.printLine("The Entropy of ", input, " is ", infoC)
2023-07-01 11:58:00 -04:00
}