1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#[cfg(test)]
mod tests;
pub struct FreqTable<'a>(pub &'a [u8]);
pub const ENGLISH_FREQ_TABLE: FreqTable<'static> = FreqTable(b"etaoinshrdlcumwfgypbvkjxqz");
pub fn most_frequent_char(text: &str) -> u8 {
let mut table = [0; 26];
let bytes = text.as_bytes();
for byte in bytes {
match *byte {
b'a'..=b'z' => table[(*byte - b'a') as usize] += 1,
b'A'..=b'Z' => table[(*byte - b'A') as usize] += 1,
_ => (),
}
}
let mut max = 0;
let mut max_index = 0;
for i in 0..table.len() {
if table[i] > max {
max = table[i];
max_index = i as u8;
}
}
max_index + b'a'
}
pub fn compute_shifts(char: u8, freq_table: &FreqTable) -> Vec<i8> {
freq_table
.0
.iter()
.map(|each| char as i8 - *each as i8)
.collect()
}