Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Entropy-Narcissist/Erlang/entropy-narcissist.erl
Normal file
24
Task/Entropy-Narcissist/Erlang/entropy-narcissist.erl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#! /usr/bin/escript
|
||||
|
||||
-define(LOG2E, 1.44269504088896340735992).
|
||||
|
||||
main(_) ->
|
||||
Self = escript:script_name(),
|
||||
{ok, Contents} = file:read_file(Self),
|
||||
io:format("My entropy is ~p~n", [entropy(Contents)]).
|
||||
|
||||
entropy(Data) ->
|
||||
Frq = count(Data),
|
||||
maps:fold(fun(_, C, E) ->
|
||||
P = C / byte_size(Data),
|
||||
E - P*math:log(P)
|
||||
end, 0, Frq) * ?LOG2E.
|
||||
|
||||
count(Data) -> count(Data, 0, #{}).
|
||||
count(Data, I, Frq) when I =:= byte_size(Data) -> Frq;
|
||||
count(Data, I, Frq) ->
|
||||
Chr = binary:at(Data, I),
|
||||
case Frq of
|
||||
#{Chr := K} -> count(Data, I+1, Frq #{Chr := K+1});
|
||||
_ -> count(Data, I+1, Frq #{Chr => 1})
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue