March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
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