Skip to content

Commit

Permalink
array.rb styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius committed Nov 22, 2015
1 parent 9ee1fdd commit 9293375
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/core_extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ def entropy
return 0 if empty?

info = {}
total = 0
each do |i|
info[i] = !info[i] ? 1 : (info[i] + 1)
total += 1
end

result = 0
result(info, length)
end

private

def result(info, total)
final = 0
info.each do |_symbol, count|
if count > 0
result += -count.to_f / total * Math.log(count.to_f / total) / Math.log(2.0)
end
next unless count > 0
percentage = count.to_f / total
final += -percentage * Math.log(percentage) / Math.log(2.0)
end
result
final
end
end

0 comments on commit 9293375

Please sign in to comment.