First commit of partial RosettaCode contents.
Pushing this for testing purposes. Lots of work still needed.
This commit is contained in:
commit
1e05ecd7ee
781 changed files with 9080 additions and 0 deletions
3
Task/Entropy/Ruby/entropy-2.rb
Normal file
3
Task/Entropy/Ruby/entropy-2.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def entropy2(s)
|
||||
s.each_char.group_by(&:to_s).values.map { |x| x.length / s.length.to_f }.reduce(0) { |e, x| e - x*Math.log2(x) }
|
||||
end
|
||||
9
Task/Entropy/Ruby/entropy.rb
Normal file
9
Task/Entropy/Ruby/entropy.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def entropy(s)
|
||||
counts = Hash.new(0)
|
||||
s.each_char { |c| counts[c] += 1 }
|
||||
|
||||
counts.values.reduce(0) do |entropy, count|
|
||||
freq = count / s.length.to_f
|
||||
entropy - freq * Math.log2(freq)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue