Sha256: 1d5bc9b35423d66e4a3f28e55549b3956d7c90020fc32e9004ce1452f6ece35b

Contents?: true

Size: 670 Bytes

Versions: 50

Compression:

Stored size: 670 Bytes

Contents

#[derive(Debug, PartialEq)]
pub enum Error {
    SpanTooLong,
    InvalidDigit(char),
}

pub fn lsp(string_digits: &str, span: usize) -> Result<u64, Error> {
    if span == 0 {
        return Ok(1);
    }

    if let Some(invalid) = string_digits.chars().find(|c| !c.is_digit(10)) {
        return Err(Error::InvalidDigit(invalid));
    }

    let products: Vec<u64> = string_digits.chars()
        .map(|c| c.to_digit(10).unwrap() as u64)
        .collect::<Vec<u64>>()
        .windows(span)
        .map(|w| w.into_iter().product())
        .collect();

    if let Some(&x) = products.iter().max() {
        Ok(x)
    } else {
        Err(Error::SpanTooLong)
    }
}

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
trackler-2.2.1.138 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.137 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.136 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.135 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.134 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.133 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.132 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.131 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.130 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.129 tracks/rust/exercises/largest-series-product/example.rs