Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
19
Task/Entropy/Lua/entropy.lua
Normal file
19
Task/Entropy/Lua/entropy.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function log2 (x) return math.log(x) / math.log(2) end
|
||||
|
||||
function entropy (X)
|
||||
local N, count, sum, i = X:len(), {}, 0
|
||||
for char = 1, N do
|
||||
i = X:sub(char, char)
|
||||
if count[i] then
|
||||
count[i] = count[i] + 1
|
||||
else
|
||||
count[i] = 1
|
||||
end
|
||||
end
|
||||
for n_i, count_i in pairs(count) do
|
||||
sum = sum + count_i / N * log2(count_i / N)
|
||||
end
|
||||
return -sum
|
||||
end
|
||||
|
||||
print(entropy("1223334444"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue