Initial commit
This commit is contained in:
2
school/intro-crypto/uebung/01/u01/.gitignore
vendored
Normal file
2
school/intro-crypto/uebung/01/u01/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
**/*.rs.bk
|
32
school/intro-crypto/uebung/01/u01/Cargo.lock
generated
Normal file
32
school/intro-crypto/uebung/01/u01/Cargo.lock
generated
Normal file
@ -0,0 +1,32 @@
|
||||
[[package]]
|
||||
name = "countmap"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "u01"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"countmap 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum countmap 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1ef2a403c4af585607826502480ab6e453f320c230ef67255eee21f0cc72c0a6"
|
||||
"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
|
||||
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
|
7
school/intro-crypto/uebung/01/u01/Cargo.toml
Normal file
7
school/intro-crypto/uebung/01/u01/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "u01"
|
||||
version = "0.1.0"
|
||||
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
||||
|
||||
[dependencies]
|
||||
countmap = "0.2.0"
|
81
school/intro-crypto/uebung/01/u01/src/main.rs
Normal file
81
school/intro-crypto/uebung/01/u01/src/main.rs
Normal file
@ -0,0 +1,81 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user