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.78 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.77 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.76 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.75 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.74 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.73 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.72 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.71 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.70 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.69 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.68 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.67 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.66 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.65 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.64 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.63 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.62 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.61 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.60 tracks/csharp/exercises/largest-series-product/Example.cs
trackler-2.2.1.59 tracks/csharp/exercises/largest-series-product/Example.cs