September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
24
Task/Entropy/Go/entropy-1.go
Normal file
24
Task/Entropy/Go/entropy-1.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main(){
|
||||
fmt.Println(H("1223334444"))
|
||||
}
|
||||
|
||||
func H(data string) (entropy float64) {
|
||||
if data == "" {
|
||||
return 0
|
||||
}
|
||||
for i := 0; i < 256; i++ {
|
||||
px := float64(strings.Count(data, string(byte(i)))) / float64(len(data))
|
||||
if px > 0 {
|
||||
entropy += -px * math.Log2(px)
|
||||
}
|
||||
}
|
||||
return entropy
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue