Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Entropy/Modula-2/entropy.mod2
Normal file
36
Task/Entropy/Modula-2/entropy.mod2
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
MODULE Entropy;
|
||||
FROM InOut IMPORT WriteString, WriteLn;
|
||||
FROM RealInOut IMPORT WriteReal;
|
||||
FROM Strings IMPORT Length;
|
||||
FROM MathLib IMPORT ln;
|
||||
|
||||
PROCEDURE entropy(s: ARRAY OF CHAR): REAL;
|
||||
VAR freq: ARRAY [0..255] OF CARDINAL;
|
||||
i, length: CARDINAL;
|
||||
h, f: REAL;
|
||||
BEGIN
|
||||
(* the entropy of the empty string is zero *)
|
||||
length := Length(s);
|
||||
IF length = 0 THEN RETURN 0.0; END;
|
||||
|
||||
(* find the frequency of each character *)
|
||||
FOR i := 0 TO 255 DO freq[i] := 0; END;
|
||||
FOR i := 0 TO length-1 DO
|
||||
INC(freq[ORD(s[i])]);
|
||||
END;
|
||||
|
||||
(* calculate the component for each character *)
|
||||
h := 0.0;
|
||||
FOR i := 0 TO 255 DO
|
||||
IF freq[i] # 0 THEN
|
||||
f := FLOAT(freq[i]) / FLOAT(length);
|
||||
h := h - f * (ln(f) / ln(2.0));
|
||||
END;
|
||||
END;
|
||||
RETURN h;
|
||||
END entropy;
|
||||
|
||||
BEGIN
|
||||
WriteReal(entropy("1223334444"), 14);
|
||||
WriteLn;
|
||||
END Entropy.
|
||||
Loading…
Add table
Add a link
Reference in a new issue