Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Entropy/Go/entropy-1.go
Normal file
25
Task/Entropy/Go/entropy-1.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main(){
|
||||
fmt.Println(H("1223334444"))
|
||||
}
|
||||
|
||||
// for ASCII strings
|
||||
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
|
||||
}
|
||||
22
Task/Entropy/Go/entropy-2.go
Normal file
22
Task/Entropy/Go/entropy-2.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const s = "1223334444"
|
||||
|
||||
l := float64(0)
|
||||
m := map[rune]float64{}
|
||||
for _, r := range s {
|
||||
m[r]++
|
||||
l++
|
||||
}
|
||||
var hm float64
|
||||
for _, c := range m {
|
||||
hm += c * math.Log2(c)
|
||||
}
|
||||
fmt.Println(math.Log2(l) - hm/l)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue