DEV Community

loading...

Discussion on: Daily Challenge #54 - What century is it?

Collapse
brightone profile image
Oleksii Filonenko

A reasonably short and reasonably Rusty solution:

pub fn century(year: u32) -> String {
    let century = year / 100 + 1;
    let suffix = match century % 100 {
        11 | 12 | 13 => "th",
        _ => match century % 10 {
            1 => "st",
            2 => "nd",
            3 => "rd",
            _ => "th",
        },
    };
    format!("{}{}", century, suffix)
}
Collapse
olekria profile image
Olek Ria

Garna robota!
Good job!

Collapse
brightone profile image
Oleksii Filonenko

Diakuyu :)
Thanks :)

Collapse
citizen428 profile image
Michael Kohl

Sometimes Rust and F# really show their common ML roots, see my solution below.

Collapse
brightone profile image
Oleksii Filonenko

I used your answer as a cheatsheet of sorts. Good job! :)