entropy.sf 283 B

1234567891011121314151617
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Entropy
  4. #
  5. func entropy(s) {
  6. var counts = Hash.new;
  7. s.each { |c| counts{c} := 0 ++ };
  8. var len = s.len;
  9. [0, counts.values.map {|count|
  10. var freq = count/len; freq * freq.log2 }...
  11. ]«-»;
  12. }
  13.  
  14. say entropy("1223334444");