re: Daily Coding Puzzles - Nov 4th - Nov 9th VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Friday The Last Word (CodeJam): You are the next contestant on this show, and the host has just showed you the string S. What's the winning last...
 

Rust

use std::io::{self, prelude::*};

fn find_last_word(s: &str) -> String {
    s.chars()
        .fold(vec![], |mut last, letter| {
            if let Some(&c) = last.last() {
                if (c as u8) > (letter as u8) {
                    last.insert(0, letter);
                } else {
                    last.push(letter);
                }
            } else {
                last.push(letter);
            }
            last
        }).iter()
        .rev()
        .into_iter()
        .collect()
}

fn main() {
    let stdin = io::stdin();

    for (i, line) in stdin.lock().lines().skip(1).enumerate() {
        if let Ok(text) = line {
            match io::stdout()
                .write(format!("Case #{}: {}\n", i + 1, find_last_word(&text)).as_bytes())
            {
                Ok(_) => (),
                Err(why) => panic!(why),
            }
        }
    }
}

usage: last_word(.exe) < small.in > small.txt

code of conduct - report abuse