Improve helper code
This commit is contained in:
parent
7be5a1e6d3
commit
5707ec957d
28
school/intro-crypto/uebung/01/u01/map
Normal file
28
school/intro-crypto/uebung/01/u01/map
Normal file
@ -0,0 +1,28 @@
|
||||
Af
|
||||
Bä
|
||||
Cg
|
||||
Dj
|
||||
Eö
|
||||
Fx
|
||||
Gn
|
||||
Ji
|
||||
Km
|
||||
Lc
|
||||
Mß
|
||||
Nv
|
||||
Od
|
||||
Pr
|
||||
Qa
|
||||
Rl
|
||||
Se
|
||||
Tü
|
||||
Uh
|
||||
Vz
|
||||
Wt
|
||||
Xw
|
||||
Yp
|
||||
Zs
|
||||
Äo
|
||||
Öb
|
||||
Üu
|
||||
ßk
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user