Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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