September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,20 +1,20 @@
|
|||
fn entropy(s: &[u8]) -> f32 {
|
||||
let mut entropy: f32 = 0.0;
|
||||
let mut histogram = [0; 256];
|
||||
let mut histogram = [0u64; 256];
|
||||
|
||||
for i in 0..s.len() {
|
||||
histogram.get_mut(s[i] as usize).map(|v| *v += 1);
|
||||
for &b in s {
|
||||
histogram[b as usize] += 1;
|
||||
}
|
||||
for i in 0..256 {
|
||||
if histogram[i] > 0 {
|
||||
let ratio = (histogram[i] as f32 / s.len() as f32) as f32;
|
||||
entropy -= (ratio * ratio.log2()) as f32;
|
||||
}
|
||||
}
|
||||
entropy
|
||||
|
||||
histogram
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|&h| h != 0)
|
||||
.map(|h| h as f32 / s.len() as f32)
|
||||
.map(|ratio| -ratio * ratio.log2())
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let arg = std::env::args().nth(1).expect("Need a string.");
|
||||
println!("Entropy of {} is {}.", arg, entropy(&arg.bytes().collect::<Vec<_>>()));
|
||||
println!("Entropy of {} is {}.", arg, entropy(arg.as_bytes()));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue