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.158 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.157 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.156 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.155 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.154 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.153 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.152 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.151 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.150 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.149 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.148 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.147 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.146 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.145 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.144 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.143 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.142 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.141 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.140 tracks/rust/exercises/largest-series-product/example.rs
trackler-2.2.1.139 tracks/rust/exercises/largest-series-product/example.rs