Anonymous user
Entropy/Narcissist: Difference between revisions
→{{header|Ruby}}: DATA & __END__ => __FILE__
m (→{{header|REXX}}: {{out}}) |
(→{{header|Ruby}}: DATA & __END__ => __FILE__) |
||
Line 287:
=={{header|Ruby}}==
<lang ruby>def entropy(s)
counts = s.
counts.values.reduce(0) do |entropy, count|
freq = count
entropy - freq * Math.log2(freq)
end
end
s = File.read(__FILE__)
p entropy(s)</lang>
{{out}}
<pre>
4.885234973253878
</pre>
|