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
|
||||
}
|
||||
|
|
@ -5,14 +5,14 @@ import (
|
|||
"math"
|
||||
)
|
||||
|
||||
const s = "1223334444"
|
||||
|
||||
func main() {
|
||||
const s = "1223334444"
|
||||
|
||||
m := map[rune]float64{}
|
||||
for _, r := range s {
|
||||
m[r]++
|
||||
}
|
||||
hm := 0.
|
||||
var fm float64
|
||||
for _, c := range m {
|
||||
hm += c * math.Log2(c)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue