Skip to content

Instantly share code, notes, and snippets.

@copyninja
Forked from rust-play/playground.rs
Last active March 19, 2018 06:50
Show Gist options
  • Save copyninja/3743e2e5500a870a5aac08d33e8bc39a to your computer and use it in GitHub Desktop.
Save copyninja/3743e2e5500a870a5aac08d33e8bc39a to your computer and use it in GitHub Desktop.
Experiment for converting from String to HashSet and perform operations (Mainly for Column Transformation cipher)
use std::collections::HashSet;
use std::str::FromStr;
fn main() {
let alphabets = String::from_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ").unwrap();
let alpha_set: HashSet<_> = alphabets.split("").filter(|x| x.len() != 0).collect();
let keyword = String::from_str("SECRET").unwrap();
let keyword_set: HashSet<_> = keyword.split("").filter(|x| x.len() != 0).collect();
let diffset: HashSet<_> = alpha_set.difference(&keyword_set).collect();
let mut rem_alphas: Vec<_> = diffset.clone().into_iter().collect();
rem_alphas.sort_by_key(|&x| alphabets.find(x));
let mut keyword_sort: Vec<_> = keyword_set.clone().into_iter().collect();
keyword_sort.sort_by_key(|&x| keyword.find(x));
let keystring: Vec<String> = keyword_sort.iter().map(|x| x.to_string()).collect();
let alpha_string: Vec<String> = rem_alphas.iter().map(|x| x.to_string()).collect();
println!("{}", keystring.join(""));
println!("{}", alpha_string.join(""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment