Skip to content

Instantly share code, notes, and snippets.

@peschkaj
Created September 20, 2017 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peschkaj/d705701d6813a377e1aaaf8ba0bafee4 to your computer and use it in GitHub Desktop.
Save peschkaj/d705701d6813a377e1aaaf8ba0bafee4 to your computer and use it in GitHub Desktop.
fn translate(source: &str) -> String {
let m = build_map();
source
.to_lowercase()
.chars()
.filter(|c| c.is_alphanumeric())
.map(|c| if c.is_numeric() { c } else { m[&c] })
.collect::<Vec<char>>()
.chunks(5)
.fold(String::new(), |mut s, chunk| {
s.push_str(" ");
s.push_str(&(*chunk).to_vec().into_iter().collect::<String>());
s
})
.trim()
.to_string()
}
fn build_map() -> HashMap<char, char> {
let plain: Vec<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect();
let mut cypher = "zyxwvutsrqponmlkjihgfedcba".chars();
plain.iter().fold(HashMap::new(), |mut map, p| {
map.insert(*p, cypher.next().unwrap());
map
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment