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.179 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.178 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.177 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.176 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.175 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.174 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.173 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.172 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.171 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.170 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.169 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.167 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.166 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.165 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.164 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.163 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.162 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.161 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.160 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.159 tracks/rust/exercises/largest-series-product/example.rs