Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::io;
- // use std::cmp::Ord;
- fn get_pos(c: char, char_map: &HashMap<char, usize>) -> f64 {
- if c.is_ascii_uppercase() {
- char_map[&c.to_ascii_lowercase()] as f64 - 0.5
- } else {
- char_map[&c] as f64
- }
- }
- fn get_char(i: f64, chars: &str) -> char {
- let mut chars = chars.chars();
- let i_ = i as usize;
- if i == i_ as f64 {
- chars.nth(i_).unwrap()
- } else {
- chars.nth(i_ + 1).unwrap().to_ascii_uppercase()
- }
- }
- fn get_sorted(order: &str, txt: &str) -> String {
- let mut char_map = HashMap::new();
- for (i, ch) in order.char_indices() {
- char_map.insert(ch, i);
- }
- let mut temp: Vec<f64> = txt.chars()
- .map(|c| get_pos(c, &char_map))
- .collect();
- temp.sort_by(|a, b| a.partial_cmp(b).unwrap());
- let s: String = temp.iter().map(|i| get_char(*i, &order)).collect();
- s
- }
- fn main() {
- let mut order = String::new();
- let mut txt = String::new();
- // read variables
- io::stdin().read_line(&mut order).ok().expect("read error");
- io::stdin().read_line(&mut txt).ok().expect("read error");
- println!("{}", get_sorted(&order, &txt));
- }
Add Comment
Please, Sign In to add comment