Published at Oct 17 2019
·
0 comments

Instructions

Test suite

Solution

Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.

For example, for the input `'1027839564'`

, the largest product for a
series of 3 digits is 270 (9 * 5 * 6), and the largest product for a
series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).

Note that these series are only required to occupy *adjacent positions*
in the input; the digits need not be *numerically consecutive*.

For the input `'73167176531330624919225119674426574742355349194934'`

,
the largest product for a series of 6 digits is 23520.

These iterators may be useful, depending on your approach

Refer to the exercism help page for Rust installation and learning resources.

Execute the tests with:

```
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests`

directory
and remove the `#[ignore]`

flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]`

flag above it.
Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

```
$ cargo test -- --ignored
```

To run a specific test, for example `some_test`

, you can use:

```
$ cargo test some_test
```

If the specific test is ignored use:

```
$ cargo test some_test -- --ignored
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

```
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```
cargo clippy --all-targets
```

Generally you should submit all files in which you implemented your solution (`src/lib.rs`

in most cases). If you are using any external crates, please consider submitting the `Cargo.toml`

file. This will make the review process faster and clearer.

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

A variation on Problem 8 at Project Euler http://projecteuler.net/problem=8

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
use largest_series_product::*;
#[test]
fn return_is_a_result() {
assert!(lsp("29", 2).is_ok());
}
#[test]
#[ignore]
fn find_the_largest_product_when_span_equals_length() {
assert_eq!(Ok(18), lsp("29", 2));
}
#[test]
#[ignore]
fn find_the_largest_product_of_two_with_numbers_in_order() {
assert_eq!(Ok(72), lsp("0123456789", 2));
}
#[test]
#[ignore]
fn find_the_largest_product_of_two_with_numbers_not_in_order() {
assert_eq!(Ok(48), lsp("576802143", 2));
}
#[test]
#[ignore]
fn find_the_largest_product_of_three_with_numbers_in_order() {
assert_eq!(Ok(504), lsp("0123456789", 3));
}
#[test]
#[ignore]
fn find_the_largest_product_of_three_with_numbers_not_in_order() {
assert_eq!(Ok(270), lsp("1027839564", 3));
}
#[test]
#[ignore]
fn find_the_largest_product_of_five_with_numbers_in_order() {
assert_eq!(Ok(15_120), lsp("0123456789", 5));
}
#[test]
#[ignore]
fn span_of_six_in_a_large_number() {
assert_eq!(
Ok(23_520),
lsp("73167176531330624919225119674426574742355349194934", 6)
);
}
#[test]
#[ignore]
fn returns_zero_if_number_is_zeros() {
assert_eq!(Ok(0), lsp("0000", 2));
}
#[test]
#[ignore]
fn returns_zero_if_all_products_are_zero() {
assert_eq!(Ok(0), lsp("99099", 3));
}
#[test]
#[ignore]
fn a_span_is_longer_than_number_is_an_error() {
assert_eq!(Err(Error::SpanTooLong), lsp("123", 4));
}
// There may be some confusion about whether this should be 1 or error.
// The reasoning for it being 1 is this:
// There is one 0-character string contained in the empty string.
// That's the empty string itself.
// The empty product is 1 (the identity for multiplication).
// Therefore LSP('', 0) is 1.
// It's NOT the case that LSP('', 0) takes max of an empty list.
// So there is no error.
// Compare against LSP('123', 4):
// There are zero 4-character strings in '123'.
// So LSP('123', 4) really DOES take the max of an empty list.
// So LSP('123', 4) errors and LSP('', 0) does NOT.
#[test]
#[ignore]
fn an_empty_string_and_no_span_returns_one() {
assert_eq!(Ok(1), lsp("", 0));
}
#[test]
#[ignore]
fn a_non_empty_string_and_no_span_returns_one() {
assert_eq!(Ok(1), lsp("123", 0));
}
#[test]
#[ignore]
fn empty_string_and_non_zero_span_is_an_error() {
assert_eq!(Err(Error::SpanTooLong), lsp("", 1));
}
#[test]
#[ignore]
fn a_string_with_non_digits_is_an_error() {
assert_eq!(Err(Error::InvalidDigit('a')), lsp("1234a5", 2));
}
```

```
#[derive(Debug, PartialEq)]
pub enum Error {
SpanTooLong,
InvalidDigit(char),
}
pub fn lsp(string_digits: &str, span: usize) -> Result<u64, Error> {
if string_digits.chars().count() < span {
return Err(Error::SpanTooLong);
}
for c in string_digits.chars() {
if !c.is_digit(10) {
return Err(Error::InvalidDigit(c));
}
}
let mut max = 0;
for i in 0..=(string_digits.chars().count() - span) {
let v = &string_digits[i..i + span];
let product = v.chars().fold(1u64, |s, a: char| s * (a as u8 - 48) as u64);
if product > max {
max = product;
}
}
Ok(max)
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,387 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments