Entropy/Narcissist: Difference between revisions
→{{header|Ruby}}: Used newer tally method, calculate size outside the loop
(Ada version) |
(→{{header|Ruby}}: Used newer tally method, calculate size outside the loop) |
||
Line 884:
=={{header|Ruby}}==
<lang ruby>def entropy(s)
counts = s.each_char.
size = s.size.to_f
counts.values.reduce(0) do |entropy, count|
freq = count /
entropy - freq * Math.log2(freq)
end
end
s = File.read(__FILE__)▼
p entropy(s)</lang>▼
▲s = File.read(__FILE__)
</lang>
{{out}}
<pre>4.653607496799478
</pre>
|