March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
18
Task/Entropy/AutoHotkey/entropy.ahk
Normal file
18
Task/Entropy/AutoHotkey/entropy.ahk
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
MsgBox, % Entropy(1223334444)
|
||||
|
||||
Entropy(n)
|
||||
{
|
||||
a := [], len := StrLen(n), m := n
|
||||
while StrLen(m)
|
||||
{
|
||||
s := SubStr(m, 1, 1)
|
||||
m := RegExReplace(m, s, "", c)
|
||||
a[s] := c
|
||||
}
|
||||
for key, val in a
|
||||
{
|
||||
m := Log(p := val / len)
|
||||
e -= p * m / Log(2)
|
||||
}
|
||||
return, e
|
||||
}
|
||||
13
Task/Entropy/CoffeeScript/entropy.coffee
Normal file
13
Task/Entropy/CoffeeScript/entropy.coffee
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
entropy = (s) ->
|
||||
freq = (s) ->
|
||||
result = {}
|
||||
for ch in s.split ""
|
||||
result[ch] ?= 0
|
||||
result[ch]++
|
||||
return result
|
||||
|
||||
frq = freq s
|
||||
n = s.length
|
||||
((frq[f]/n for f of frq).reduce ((e, p) -> e - p * Math.log(p)), 0) * Math.LOG2E
|
||||
|
||||
console.log "The entropy of the string '1223334444' is #{entropy '1223334444'}"
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
import std.stdio, std.algorithm, std.math;
|
||||
|
||||
double entropy(T)(T[] s)
|
||||
/*pure nothrow*/ if (__traits(compiles, sort(s))) {
|
||||
/*pure nothrow*/ if (__traits(compiles, s.sort())) {
|
||||
return s
|
||||
.sort()
|
||||
.group
|
||||
.map!(g => g[1] / cast(double)s.length)
|
||||
.map!(p => -p * log2(p))
|
||||
.reduce!q{a + b};
|
||||
.map!(g => g[1] / double(s.length))
|
||||
.map!(p => -p * p.log2)
|
||||
.sum;
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
|||
16
Task/Entropy/Rust/entropy.rust
Normal file
16
Task/Entropy/Rust/entropy.rust
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// works for Rust 0.9
|
||||
fn entropy(s: &str) -> f32 {
|
||||
let mut entropy: f32 = 0.0;
|
||||
let mut histogram = [0, ..256];
|
||||
let len = s.len();
|
||||
|
||||
for i in range(0, len) { histogram[s[i]] += 1; }
|
||||
for i in range(0, 256) {
|
||||
if histogram[i] > 0 {
|
||||
let ratio = (histogram[i] as f32 / len as f32) as f32;
|
||||
entropy -= (ratio * log2(ratio)) as f32;
|
||||
}
|
||||
}
|
||||
|
||||
entropy
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue