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.0.5 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.0.4 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.0.3 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.0.2 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.0.1 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.0.0.0 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.4.1 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.4.0 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.3.0 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.2.1 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.2.0 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.1.2 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.1.1 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.1.0 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.0.1 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-1.0.0 tracks/csharp/exercises/largest-series-product/Example.cs