Sha256: 3bc4e0c120cbb96e64614ec949efb86891aa653b44931a22cd5c918cc36ec3d4

Contents?: true

Size: 1.99 KB

Versions: 26

Compression:

Stored size: 1.99 KB

Contents

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

public static class PalindromeProducts
{
    public static (int, IEnumerable<(int,int)>) Largest(int minFactor, int maxFactor)
    {
        return FindPalindrome(minFactor, maxFactor, x => x.Max(p => p.Item1));
    }

    public static (int, IEnumerable<(int,int)>) Smallest(int minFactor, int maxFactor)
    {
        return FindPalindrome(minFactor, maxFactor, x => x.Min(p => p.Item1));
    }

    private static (int, IEnumerable<(int, int)>) FindPalindrome(int minFactor, int maxFactor,
        Func<List<(int, (int, int))>, int> valueSelector)
    {
        if (minFactor > maxFactor || maxFactor < minFactor)
        {
            throw new ArgumentException();    
        }
        
        try
        {
            var palindromes = FindAllPalindromes(minFactor, maxFactor);
            var value = valueSelector(palindromes);
            var factors = new HashSet<(int, int)>(palindromes.Where(p => p.Item1 == value).Select(p => p.Item2));
            return (value, factors);
        } catch (InvalidOperationException)
        {
            throw new ArgumentException();
        }
    }

    private static List<(int, (int, int))> FindAllPalindromes(int minFactor, int maxFactor)
    {
        return (from pair in Pairs(minFactor, maxFactor)
                let product = pair.Item1 * pair.Item2
                where IsPalindrome(product)
                select (product, pair))
                .ToList();
    }

    private static IEnumerable<(int, int)> Pairs(int minFactor, int maxFactor)
    {
        return from x in Enumerable.Range(minFactor, maxFactor + 1 - minFactor)
               from y in Enumerable.Range(x, maxFactor + 1 - x)
               select (x, y);
    }

    private static bool IsPalindrome(int num)
    {
        var n = num;
        var rev = 0;
        while (num > 0)
        {
            var dig = num % 10;
            rev = rev * 10 + dig;
            num = num / 10;
        }

        return n == rev;
    }
}

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/palindrome-products/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/palindrome-products/Example.cs