Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
14
Task/Entropy/OCaml/entropy-1.ml
Normal file
14
Task/Entropy/OCaml/entropy-1.ml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
module CharMap = Map.Make(Char)
|
||||
|
||||
let entropy s =
|
||||
let count map c =
|
||||
CharMap.update c (function Some n -> Some (n +. 1.) | None -> Some 1.) map
|
||||
and calc _ n sum =
|
||||
sum +. n *. Float.log2 n
|
||||
in
|
||||
let sum = CharMap.fold calc (String.fold_left count CharMap.empty s) 0.
|
||||
and len = float (String.length s) in
|
||||
Float.log2 len -. sum /. len
|
||||
|
||||
let () =
|
||||
entropy "1223334444" |> string_of_float |> print_endline
|
||||
Loading…
Add table
Add a link
Reference in a new issue