# artemkorsakov's solution

## to Largest Series Product in the C# Track

Published at Feb 08 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.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit LargestSeriesProduct.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

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

## Submitting Incomplete Solutions

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

### LargestSeriesProductTest.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using System;
using Xunit;

public class LargestSeriesProductTest
{
[Fact]
public void Finds_the_largest_product_if_span_equals_length()
{
Assert.Equal(18, LargestSeriesProduct.GetLargestProduct("29", 2));
}

[Fact(Skip = "Remove to run test")]
public void Can_find_the_largest_product_of_2_with_numbers_in_order()
{
Assert.Equal(72, LargestSeriesProduct.GetLargestProduct("0123456789", 2));
}

[Fact(Skip = "Remove to run test")]
public void Can_find_the_largest_product_of_2()
{
Assert.Equal(48, LargestSeriesProduct.GetLargestProduct("576802143", 2));
}

[Fact(Skip = "Remove to run test")]
public void Can_find_the_largest_product_of_3_with_numbers_in_order()
{
Assert.Equal(504, LargestSeriesProduct.GetLargestProduct("0123456789", 3));
}

[Fact(Skip = "Remove to run test")]
public void Can_find_the_largest_product_of_3()
{
Assert.Equal(270, LargestSeriesProduct.GetLargestProduct("1027839564", 3));
}

[Fact(Skip = "Remove to run test")]
public void Can_find_the_largest_product_of_5_with_numbers_in_order()
{
Assert.Equal(15120, LargestSeriesProduct.GetLargestProduct("0123456789", 5));
}

[Fact(Skip = "Remove to run test")]
public void Can_get_the_largest_product_of_a_big_number()
{
Assert.Equal(23520, LargestSeriesProduct.GetLargestProduct("73167176531330624919225119674426574742355349194934", 6));
}

[Fact(Skip = "Remove to run test")]
public void Reports_zero_if_the_only_digits_are_zero()
{
Assert.Equal(0, LargestSeriesProduct.GetLargestProduct("0000", 2));
}

[Fact(Skip = "Remove to run test")]
public void Reports_zero_if_all_spans_include_zero()
{
Assert.Equal(0, LargestSeriesProduct.GetLargestProduct("99099", 3));
}

[Fact(Skip = "Remove to run test")]
public void Rejects_span_longer_than_string_length()
{
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct("123", 4));
}

[Fact(Skip = "Remove to run test")]
public void Reports_1_for_empty_string_and_empty_product_0_span_()
{
Assert.Equal(1, LargestSeriesProduct.GetLargestProduct("", 0));
}

[Fact(Skip = "Remove to run test")]
public void Reports_1_for_nonempty_string_and_empty_product_0_span_()
{
Assert.Equal(1, LargestSeriesProduct.GetLargestProduct("123", 0));
}

[Fact(Skip = "Remove to run test")]
public void Rejects_empty_string_and_nonzero_span()
{
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct("", 1));
}

[Fact(Skip = "Remove to run test")]
public void Rejects_invalid_character_in_digits()
{
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct("1234a5", 2));
}

[Fact(Skip = "Remove to run test")]
public void Rejects_negative_span()
{
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct("12345", -1));
}
}``````
``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

public static class LargestSeriesProduct
{
public static long GetLargestProduct(string digits, int span)
{
CheckParameters(digits, span);
var products = new List<long>();
for (var i = 0; i <= digits.Length - span; i++)
{
}
return products.Max();
}

private static long GetProduct(string digits)
{
return digits.Select(char.GetNumericValue).Aggregate(1L, (t, n) => t*(long)n);
}

private static void CheckParameters(string digits, int span)
{
if (span < 0)
{
throw new ArgumentException("span should not be negative");
}
if (digits.Length < span)
{
throw new ArgumentException("span must be less than digits.Length");
}
if (!new Regex(@"^\d*\$").IsMatch(digits))
{
throw new ArgumentException("invalid character in digits");
}
}
}``````