Sha256: 76d2b7758ef2f56c9e4a135871b89783ca0fda1bc58219aee02704164133100f

Contents?: true

Size: 1.11 KB

Versions: 396

Compression:

Stored size: 1.11 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public static class LargestSeriesProduct
{
    public static long GetLargestProduct(string digits, int span) => GetSlices(ParseDigits(digits), span).Max(l => GetProduct(l));

    private static IEnumerable<IEnumerable<long>> GetSlices(long[] digits, int span)
    {
        if (span < 0 || span > digits.Length)
        {
            throw new ArgumentException("Invalid span.");
        }

        return Enumerable.Range(0, GetNumberOfSlices(digits, span)).Select(i => digits.Skip(i).Take(span));
    }

    private static long[] ParseDigits(string digits) => digits.ToCharArray().Select(ParseDigit).ToArray();

    private static long ParseDigit(char c)
    {
        if (!char.IsDigit(c))
        {
            throw new ArgumentException("Invalid digit.");
        }

        return long.Parse(c.ToString());
    }

    private static long GetProduct(IEnumerable<long> numbers) => numbers.Aggregate(1L, (x, product) => x * product);

    private static int GetNumberOfSlices(long[] digits, int span) => digits.Length + 1 - span;
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.6.40 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.39 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.38 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.37 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.36 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.35 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.34 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.33 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.32 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.31 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.30 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.29 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.28 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.27 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.26 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.25 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.24 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.23 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.22 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.6.21 tracks/csharp/exercises/largest-series-product/Example.cs