Entropy: Difference between revisions

651 bytes added ,  2 years ago
no edit summary
(Added Arturo implementation)
No edit summary
Line 3,158:
entropy = 1.84644
entropy = 3.08496
</pre>
 
=={{header|Go}}==
 
===Vlang: Map version===
<lang vlang>import math
import arrays
 
fn hist(source string) map[string]int {
mut hist := map[string]int{}
for e in source.split('') {
if e !in hist {
hist[e] = 0
}
hist[e]+=1
}
return hist
}
 
fn entropy(hist map[string]int, l int) f64 {
mut elist := []f64{}
for _,v in hist {
c := f64(v) / f64(l)
elist << -c * math.log2(c)
}
return arrays.sum<f64>(elist) or {-1}
}
 
fn main(){
input := "1223334444"
h := hist(input)
e := entropy(h, input.len)
println(e)
}</lang>
{{out}}
<pre>
1.8464393446710152
</pre>
 
338

edits