RosettaCodeData/Task/Entropy/Go/entropy-2.go

22 lines
315 B
Go
Raw Permalink Normal View History

2013-10-27 22:24:23 +00:00
package main
import (
"fmt"
"math"
)
func main() {
2017-09-23 10:01:46 +02:00
const s = "1223334444"
2013-10-27 22:24:23 +00:00
m := map[rune]float64{}
for _, r := range s {
m[r]++
}
2017-09-23 10:01:46 +02:00
var fm float64
2013-10-27 22:24:23 +00:00
for _, c := range m {
hm += c * math.Log2(c)
}
const l = float64(len(s))
fmt.Println(math.Log2(l) - hm/l)
}