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.98 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.97 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.96 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.95 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.94 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.93 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.92 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.91 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.90 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.89 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.88 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.87 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.86 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.85 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.84 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.83 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.82 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.81 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.80 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.79 tracks/csharp/exercises/largest-series-product/Example.cs