 ## to Palindrome Products in the C# Track

Published at Jun 14 2019 · 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

### PalindromeProductsTest.cs

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

using System;
using Xunit;

public class PalindromeProductsTest
{
[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 to run 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 to run 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 to run 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 to run 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 to run 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 to run 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 to run 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 to run test")]
public void Empty_result_for_smallest_if_no_palindrome_in_the_range()
{
Assert.Throws<ArgumentException>(() => PalindromeProducts.Smallest(1002, 1003));
}

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

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

[Fact(Skip = "Remove to run 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
{
public static (int, IEnumerable<(int, int)>) Largest(int minFactor, int maxFactor)
{
MinMoreThanMax(minFactor, maxFactor);
var largestPalindrome = GetPalindrome(minFactor, maxFactor, "largest");
var factors = GetFactors(largestPalindrome, maxFactor.ToString().Length);
return (largestPalindrome, factors);
}

public static (int, IEnumerable<(int, int)>) Smallest(int minFactor, int maxFactor)
{
MinMoreThanMax(minFactor, maxFactor);
var smallestPalindrome = GetPalindrome(minFactor, maxFactor, "smallest");
var factors = GetFactors(smallestPalindrome, minFactor.ToString().Length);
return (smallestPalindrome, factors);
}

private static int GetPalindrome(int start, int end, string factors)
{
var palindromes = new List<int>();
var products = GetAllProducts(start, end);
foreach (var p in products)
{
if (IsPalindrome(p))
{
}
}
if (palindromes.Count() == 0)
{
throw new ArgumentException();

}
return factors == "largest" ? palindromes.Max() : palindromes.Min();
}

private static IEnumerable<int> GetAllProducts(int start, int end)
{
var products = new List<int>();
for (int i = start; i <= end; i++)
{
for (int j = i; j <= end; j++)
{
}
}
return products;
}

private static bool IsPalindrome(int number)
{
int reverse = 0, temp = number;
while (temp != 0)
{
reverse *= 10;
reverse += temp % 10;
temp /= 10;
}
return number == reverse;
}
private static (int, int)[] GetFactors(int palindrome, int digitLenght)
{
var factors = new List<(int, int)>();
for (var i = palindrome; i >= 1; i--)
{
if (palindrome % i == 0)
{
if (i.ToString().Length == digitLenght && (palindrome / i).ToString().Length == digitLenght)
{
}
}
}
return factors.Where(a => a.Item1 <= a.Item2).ToArray();
}

private static void MinMoreThanMax(int min, int max)
{
if (min > max)
{
throw new ArgumentException();
}
}
}``````