RosettaCodeData/Task/Entropy/Nim/entropy.nim

9 lines
193 B
Nim
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import tables, math
proc entropy(s: string): float =
var t = initCountTable[char]()
for c in s: t.inc(c)
for x in t.values: result -= x/s.len * log2(x/s.len)
echo entropy("1223334444")