🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # MrWulf87's solution

## to Palindrome Products in the C# Track

Published at Jun 27 2020 · 0 comments
Instructions
Test suite
Solution

Detect palindrome products in a given range.

A palindromic number is a number that remains the same when its digits are reversed. For example, `121` is a palindromic number but `112` is not.

Given a range of numbers, find the largest and smallest palindromes which are products of numbers within that range.

Your solution should return the largest and smallest palindromes, along with the factors of each within the range. If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.

## Example 1

Given the range `[1, 9]` (both inclusive)...

And given the list of all possible products within this range: `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]`

The palindrome products are all single digit numbers (in this case): `[1, 2, 3, 4, 5, 6, 7, 8, 9]`

The smallest palindrome product is `1`. Its factors are `(1, 1)`. The largest palindrome product is `9`. Its factors are `(1, 9)` and `(3, 3)`.

## Example 2

Given the range `[10, 99]` (both inclusive)...

The smallest palindrome product is `121`. Its factors are `(11, 11)`. The largest palindrome product is `9009`. Its factors are `(91, 99)`.

## Hints

For this exercise, you will need to create a set of factors using tuples. For more information on tuples, see this link.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit PalindromeProducts.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

Problem 4 at Project Euler http://projecteuler.net/problem=4

### PalindromeProductsTests.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using System;
using Xunit;

public class PalindromeProductsTests
{
[Fact]
public void Finds_the_smallest_palindrome_from_single_digit_factors()
{
var actual = PalindromeProducts.Smallest(1, 9);
var expected = (1, new[] { (1, 1) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Finds_the_largest_palindrome_from_single_digit_factors()
{
var actual = PalindromeProducts.Largest(1, 9);
var expected = (9, new[] { (1, 9), (3, 3) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_smallest_palindrome_from_double_digit_factors()
{
var actual = PalindromeProducts.Smallest(10, 99);
var expected = (121, new[] { (11, 11) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_largest_palindrome_from_double_digit_factors()
{
var actual = PalindromeProducts.Largest(10, 99);
var expected = (9009, new[] { (91, 99) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_smallest_palindrome_from_triple_digit_factors()
{
var actual = PalindromeProducts.Smallest(100, 999);
var expected = (10201, new[] { (101, 101) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_largest_palindrome_from_triple_digit_factors()
{
var actual = PalindromeProducts.Largest(100, 999);
var expected = (906609, new[] { (913, 993) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_smallest_palindrome_from_four_digit_factors()
{
var actual = PalindromeProducts.Smallest(1000, 9999);
var expected = (1002001, new[] { (1001, 1001) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Find_the_largest_palindrome_from_four_digit_factors()
{
var actual = PalindromeProducts.Largest(1000, 9999);
var expected = (99000099, new[] { (9901, 9999) });
Assert.Equal(expected.Item1, actual.Item1);
Assert.Equal(expected.Item2, actual.Item2);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Empty_result_for_smallest_if_no_palindrome_in_the_range()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Smallest(1002, 1003));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Empty_result_for_largest_if_no_palindrome_in_the_range()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Largest(15, 15));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Error_result_for_smallest_if_min_is_more_than_max()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Smallest(10000, 1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Error_result_for_largest_if_min_is_more_than_max()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Largest(2, 1));
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.Linq;

public static class PalindromeProducts
{
private static (int, IEnumerable<(int, int)>) findPalindromeProducts (int minFactor, int maxFactor, bool smallest)
{
if (minFactor > maxFactor)
throw new ArgumentException();
var palindromes = Palindromes(minFactor, maxFactor);
if (!palindromes.Any())
throw new ArgumentException();
if(smallest)
{
int minPalindromeProduct = palindromes.Select(e => e.Item1).Min();
return (minPalindromeProduct, palindromes.Where(e => e.Item1 == minPalindromeProduct).Select(e => (e.Item2, e.Item3)).OrderBy(e => e));
}
else
{
int maxPalindromeProduct = palindromes.Select(e => e.Item1).Max();
return (maxPalindromeProduct, palindromes.Where(e => e.Item1 == maxPalindromeProduct).Select(e => (e.Item2, e.Item3)).OrderBy(e => e));
}
}

private static IEnumerable<(int, int, int)> Palindromes(int minFactor, int maxFactor)
{
return
from i in Enumerable.Range(minFactor, maxFactor - minFactor + 1).AsParallel().AsOrdered()
from j in Enumerable.Range(minFactor, i - minFactor + 1).AsParallel().AsOrdered()
let product = j * i
where IsPalindrome(product)
select (product, j, i);
}

private static bool IsPalindrome (int product)
{
int original = product;
int reverse = 0;
while (product > 0)
{
reverse = reverse * 10 + product % 10;
product /= 10;
}
return original == reverse;
}

public static (int, IEnumerable<(int, int)>) Smallest(int minFactor, int maxFactor) => findPalindromeProducts(minFactor, maxFactor, true);
public static (int, IEnumerable<(int, int)>) Largest(int minFactor, int maxFactor) => findPalindromeProducts(minFactor, maxFactor, false);

}``````