taesik

Largest 5 digit number in a series 본문

functions and trick/RUST

Largest 5 digit number in a series

taesikk 2022. 6. 6. 17:43
fn largest_five_digit_number(num: &str) -> u32 {
    (0..num.len() - 4).filter_map(|start| num[start..start+5].parse::<u32>().ok()).max().unwrap()
}

In the following 6 digit number:

283910

91 is the greatest sequence of 2 consecutive digits.

In the following 10 digit number:

1234567890

67890 is the greatest sequence of 5 consecutive digits.

 

fn largest_five_digit_number(num: &str) -> u32 {
    (0..num.len() - 4).filter_map(|start| num[start..start+5].parse::<u32>().ok()).max().unwrap()
}

'functions and trick > RUST' 카테고리의 다른 글

return the number of vowels  (0) 2022.06.09
Masking with # except last 4 string  (0) 2022.06.09
Break camelCase  (0) 2022.06.06