RosettaCodeData/Task/Entropy/Ruby/entropy-1.rb

13 lines
238 B
Ruby
Raw Permalink Normal View History

2013-04-10 12:38:42 -07:00
def entropy(s)
2015-11-18 06:14:39 +00:00
counts = Hash.new(0.0)
2013-04-10 12:38:42 -07:00
s.each_char { |c| counts[c] += 1 }
2015-11-18 06:14:39 +00:00
leng = s.length
2013-04-10 12:38:42 -07:00
counts.values.reduce(0) do |entropy, count|
2015-11-18 06:14:39 +00:00
freq = count / leng
2013-04-10 12:38:42 -07:00
entropy - freq * Math.log2(freq)
end
end
2015-11-18 06:14:39 +00:00
p entropy("1223334444")