Improve helper code

This commit is contained in:
Valentin Brandl 2018-10-15 10:38:22 +02:00
parent 7be5a1e6d3
commit 5707ec957d
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
2 changed files with 54 additions and 35 deletions

View File

@ -0,0 +1,28 @@
Af
Cg
Dj
Fx
Gn
Ji
Km
Lc
Nv
Od
Pr
Qa
Rl
Se
Uh
Vz
Wt
Xw
Yp
Zs
Äo
Öb
Üu
ßk

View File

@ -2,13 +2,15 @@ extern crate countmap;
use countmap::CountMap;
use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader},
path::Path,
};
fn count() {
let args: Vec<_> = std::env::args().collect();
let file = args.get(1).unwrap();
let file = &args[1];
let mut map: CountMap<char, u32> = CountMap::new();
let read = BufReader::new(File::open(file).unwrap());
for line in read.lines() {
@ -30,51 +32,40 @@ fn count() {
"{} &\\text{{:}}& {:3} \\to{{}} {:.2} \\\\",
k,
v,
v as f64 / sum as f64
f64::from(v) / f64::from(sum)
)
});
println!("sum &\\text{{:}}& {}", sum);
}
fn create_map<P: AsRef<Path>>(path: P) -> std::io::Result<HashMap<char, char>> {
let read = BufReader::new(File::open(path)?);
let mut map = HashMap::new();
for line in read.lines() {
if let Ok(line) = line {
let chars: Vec<char> = line.chars().collect();
if chars.len() != 2 {
eprintln!("Invalid line {}", line);
} else {
map.insert(chars[0], chars[1]);
}
}
}
Ok(map)
}
fn main() {
count();
let args: Vec<_> = std::env::args().collect();
let file = args.get(1).unwrap();
let read = BufReader::new(File::open(file).unwrap());
let target = &args[1];
let map = create_map(&args[2]).unwrap();
let read = BufReader::new(File::open(target).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();
.map(|c| map.get(&c).cloned().unwrap_or(c))
.collect();
println!("{}", s);
}
}