array.rb styles

This commit is contained in:
Danielius
2015-11-22 15:57:27 +02:00
parent 9ee1fdd0ee
commit 9293375bda

View File

@@ -8,18 +8,22 @@ class Array
return 0 if empty? return 0 if empty?
info = {} info = {}
total = 0
each do |i| each do |i|
info[i] = !info[i] ? 1 : (info[i] + 1) info[i] = !info[i] ? 1 : (info[i] + 1)
total += 1
end end
result = 0 result(info, length)
end
private
def result(info, total)
final = 0
info.each do |_symbol, count| info.each do |_symbol, count|
if count > 0 next unless count > 0
result += -count.to_f / total * Math.log(count.to_f / total) / Math.log(2.0) percentage = count.to_f / total
end final += -percentage * Math.log(percentage) / Math.log(2.0)
end end
result final
end end
end end