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.2.1.180 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/largest-series-product/Example.cs