RosettaCodeData/Task/Entropy/Elixir/entropy.elixir

15 lines
346 B
Text
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
defmodule RC do
def entropy(str) do
leng = String.length(str)
2016-12-05 22:15:40 +01:00
String.graphemes(str)
2015-11-18 06:14:39 +00:00
|> Enum.group_by(&(&1))
|> Enum.map(fn{_,value} -> length(value) end)
|> Enum.reduce(0, fn count, entropy ->
freq = count / leng
entropy - freq * :math.log2(freq)
end)
end
end
IO.inspect RC.entropy("1223334444")