loading...

re: Solving a Job Application Code Challenge VIEW POST

FULL DISCUSSION
 

Probably not the most elegant way... I really need more practice with Rust :D

fn main() {
    let blob = "...";

    let mut result = blob
        .chars()
        .fold(vec![(None, 0); 27], |mut counter, c| {
            if c == '_' {
                if counter[26].0.is_none() { counter[26].0 = Some('_'); }
                counter[26].1 += 1;
            }
            else {
                let pos = (c as u8 - 'a' as u8) as usize;

                if counter[pos].0.is_none() { counter[pos].0 = Some(c); }
                counter[pos].1 += 1;
            }

            counter
        })
    ;

    result.sort_by(|a,b| b.1.cmp(&a.1));
    let result = result
        .iter()
        .fold(String::new(), |mut s, c| { s.push(c.0.unwrap_or(' ')); s })
    ;

    let result: Vec<&str> = result
        .split('_')
        .collect()
    ;

    println!("{}", result[0]);
}
Code of Conduct Report abuse