30 lines
672 B
Plaintext
30 lines
672 B
Plaintext
$ include "seed7_05.s7i";
|
|
include "float.s7i";
|
|
include "math.s7i";
|
|
|
|
const func float: entropy (in string: stri) is func
|
|
result
|
|
var float: entropy is 0.0;
|
|
local
|
|
var hash [char] integer: count is (hash [char] integer).value;
|
|
var char: ch is ' ';
|
|
var float: p is 0.0;
|
|
begin
|
|
for ch range stri do
|
|
if ch in count then
|
|
incr(count[ch]);
|
|
else
|
|
count @:= [ch] 1;
|
|
end if;
|
|
end for;
|
|
for key ch range count do
|
|
p := flt(count[ch]) / flt(length(stri));
|
|
entropy -:= p * log(p) / log(2.0);
|
|
end for;
|
|
end func ;
|
|
|
|
const proc: main is func
|
|
begin
|
|
writeln(entropy("1223334444") digits 5);
|
|
end func;
|