🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Palindrome Products in the Scala Track

Published at Sep 09 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).

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

PalindromeProductsTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class PalindromeProductsTest extends FunSuite with Matchers {

  // PalindromeProducts largest call is expecting a return type of
  // Option[(Int, Set[(Int, Int)])] - None is expected for error cases.
  // Some is expected for valid cases. The first element of the tuple 
  // is the largest palindrome product value. The second element of the
  // tuple is the list of factors.
  // PalindromeProducts smallest call is expecting a return type of
  // Option[(Int, Set[(Int, Int)])] - None is expected for error cases.
  // Some is expected for valid cases. The first element of the tuple 
  // is the smallest palindrome product value. The second element of the
  // tuple is the list of factors.

  test("finds the smallest palindrome from single digit factors") {
    PalindromeProducts(1, 9).smallest should be (Some((1, Set((1, 1)))))
  }

  test("finds the largest palindrome from single digit factors") {
    pending
    PalindromeProducts(1, 9).largest should be (Some((9, Set((1, 9), (3, 3)))))
  }

  test("find the smallest palindrome from double digit factors") {
    pending
    PalindromeProducts(10, 99).smallest should be (Some((121, Set((11, 11)))))
  }

  test("find the largest palindrome from double digit factors") {
    pending
    PalindromeProducts(10, 99).largest should be (Some((9009, Set((91, 99)))))
  }

  test("find smallest palindrome from triple digit factors") {
    pending
    PalindromeProducts(100, 999).smallest should be (Some((10201, Set((101, 101)))))
  }

  test("find the largest palindrome from triple digit factors") {
    pending
    PalindromeProducts(100, 999).largest should be (Some((906609, Set((913, 993)))))
  }

  test("find smallest palindrome from four digit factors") {
    pending
    PalindromeProducts(1000, 9999).smallest should be (Some((1002001, Set((1001, 1001)))))
  }

  test("find the largest palindrome from four digit factors") {
    pending
    PalindromeProducts(1000, 9999).largest should be (Some((99000099, Set((9901, 9999)))))
  }

  test("empty result for smallest if no palindrome in the range") {
    pending
    PalindromeProducts(1002, 1003).smallest should be (None)
  }

  test("empty result for largest if no palindrome in the range") {
    pending
    PalindromeProducts(15, 15).largest should be (None)
  }

  test("error result for smallest if min is more than max") {
    pending
    PalindromeProducts(10000, 1).smallest should be (None)
  }

  test("error result for largest if min is more than max") {
    pending
    PalindromeProducts(2, 1).largest should be (None)
  }
}
import PalindromeProducts._

import scala.util.Try

case class PalindromeProducts(start: Int, end: Int) {
  private lazy val factoredPairsProduct: Map[Int, Set[(Int, Int)]] = (for {
    i <- (start to end).toSet[Int]
    j <- i to end if isPalindrome(i * j)
  } yield (i, j)) groupBy (x => x._1 * x._2)
  lazy val smallest = Try(factoredPairsProduct.minBy(_._1)).toOption
  lazy val largest = Try(factoredPairsProduct.maxBy(_._1)).toOption
}

object PalindromeProducts {
  def isPalindrome(num: Int): Boolean = {
    num == reverse(num)
  }

  def reverse(n: Int, f: Int = 0): Int = n match {
    case 0 => f
    case _ => reverse(n / 10, f * 10 + n % 10)
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?