82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
|
extern crate countmap;
|
||
|
|
||
|
use countmap::CountMap;
|
||
|
use std::{
|
||
|
fs::File,
|
||
|
io::{BufRead, BufReader},
|
||
|
};
|
||
|
|
||
|
fn count() {
|
||
|
let args: Vec<_> = std::env::args().collect();
|
||
|
let file = args.get(1).unwrap();
|
||
|
let mut map: CountMap<char, u32> = CountMap::new();
|
||
|
let read = BufReader::new(File::open(file).unwrap());
|
||
|
for line in read.lines() {
|
||
|
if let Ok(line) = line {
|
||
|
line.chars().filter(|c| c.is_alphabetic()).for_each(|c| {
|
||
|
map.insert_or_increment(c);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
let sum: u32 = map
|
||
|
.iter()
|
||
|
.filter(|(k, _)| k.is_alphabetic())
|
||
|
.map(|(_, v)| v)
|
||
|
.sum();
|
||
|
let mut vec: Vec<_> = map.into_iter().collect();
|
||
|
vec.sort_unstable();
|
||
|
vec.into_iter().for_each(|(k, v)| {
|
||
|
println!(
|
||
|
"{} &\\text{{:}}& {:3} \\to{{}} {:.2} \\\\",
|
||
|
k,
|
||
|
v,
|
||
|
v as f64 / sum as f64
|
||
|
)
|
||
|
});
|
||
|
println!("sum &\\text{{:}}& {}", sum);
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let args: Vec<_> = std::env::args().collect();
|
||
|
let file = args.get(1).unwrap();
|
||
|
let read = BufReader::new(File::open(file).unwrap());
|
||
|
for line in read.lines() {
|
||
|
if let Ok(line) = line {
|
||
|
let s: String = line
|
||
|
.chars()
|
||
|
.map(|c| match c {
|
||
|
'A' => 'f',
|
||
|
'B' => 'ä',
|
||
|
'C' => 'g',
|
||
|
'D' => 'j',
|
||
|
'E' => 'ö',
|
||
|
'F' => 'x',
|
||
|
'G' => 'n',
|
||
|
'J' => 'i',
|
||
|
'K' => 'm',
|
||
|
'L' => 'c',
|
||
|
'M' => 'ß',
|
||
|
'N' => 'v',
|
||
|
'O' => 'd',
|
||
|
'P' => 'r',
|
||
|
'Q' => 'a',
|
||
|
'R' => 'l',
|
||
|
'S' => 'e',
|
||
|
'T' => 'ü',
|
||
|
'U' => 'h',
|
||
|
'V' => 'z',
|
||
|
'W' => 't',
|
||
|
'X' => 'w',
|
||
|
'Y' => 'p',
|
||
|
'Z' => 's',
|
||
|
'Ä' => 'o',
|
||
|
'Ö' => 'b',
|
||
|
'Ü' => 'u',
|
||
|
'ß' => 'k',
|
||
|
c => c,
|
||
|
}).collect();
|
||
|
println!("{}", s);
|
||
|
}
|
||
|
}
|
||
|
}
|