Skip to content

Create PalindromeProductsTestGenerator.scala. Update PalindromeProduc… #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@
"unlocked_by": "sum-of-multiples",
"difficulty": 6,
"topics": [
"Optional values",
"Sets",
"Strings",
"Tuples",
Expand Down
11 changes: 7 additions & 4 deletions exercises/palindrome-products/example.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@

case class PalindromeProducts(minFactor: Int, maxFactor: Int) {

lazy val (smallest: (Int, Set[(Int, Int)]),
largest: (Int, Set[(Int, Int)])) = {
lazy val (smallest: Option[(Int, Set[(Int, Int)])],
largest: Option[(Int, Set[(Int, Int)])]) = {
val palindromes = for (a <- Range(minFactor, maxFactor + 1);
b <- Range(a, maxFactor + 1) if isPalindrome(a * b)) yield (a * b, (a, b))
b <- Range(a, maxFactor + 1) if isPalindrome(a * b)) yield (a * b, (a, b))
val mapped = palindromes.groupBy(_._1).map{case(k, v) => (k, v.map(_._2).toSet)}
(mapped.minBy(_._1), mapped.maxBy(_._1))
if (mapped.isEmpty)
(None, None)
else
(Some(mapped.minBy(_._1)), Some(mapped.maxBy(_._1)))
}

private def isPalindrome(i: Int) = i.toString == i.toString.reverse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,79 @@
import org.scalatest.{Matchers, FlatSpec}
import org.scalatest.{Matchers, FunSuite}

class PalindromeProductsTest extends FlatSpec with Matchers {
/** @version 1.0.0 */
class PalindromeProductsTest extends FunSuite with Matchers {

it should "find smallest palindrome from a single digit factors" in {
val (value, factors) = PalindromeProducts(1, 9).smallest
value should be (1)
factors should be (Set((1, 1)))
// 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)))))
}

it should "find largest palindrome from a single digit factors" in {
test("find the largest palindrome from triple digit factors") {
pending
val (value, factors) = PalindromeProducts(0, 9).largest
value should be (9)
factors should be (Set((1, 9), (3, 3)))
PalindromeProducts(100, 999).largest should be(
Some((906609, Set((913, 993)))))
}

it should "find smallest palindrome from a double digit factors" in {
test("find smallest palindrome from four digit factors") {
pending
val (value, factors) = PalindromeProducts(10, 99).smallest
value should be (121)
factors should be (Set((11, 11)))
PalindromeProducts(1000, 9999).smallest should be(
Some((1002001, Set((1001, 1001)))))
}

it should "find largest palindrome from a double digit factors" in {
test("find the largest palindrome from four digit factors") {
pending
val (value, factors) = PalindromeProducts(10, 99).largest
value should be (9009)
factors should be (Set((91, 99)))
PalindromeProducts(1000, 9999).largest should be(
Some((99000099, Set((9901, 9999)))))
}

it should "find smallest palindrome from a triple digit factors" in {
test("empty result for smallest if no palindrome in the range") {
pending
val (value, factors) = PalindromeProducts(100, 999).smallest
value should be (10201)
factors should be (Set((101, 101)))
PalindromeProducts(1002, 1003).smallest should be(None)
}

it should "find largest palindrome from a triple digit factors" in {
test("empty result for largest if no palindrome in the range") {
pending
val (value, factors) = PalindromeProducts(100, 999).largest
value should be (906609)
factors should be (Set((913, 993)))
PalindromeProducts(15, 15).largest should be(None)
}

it should "find smallest palindrome from a four digit factors" in {
test("error result for smallest if min is more than max") {
pending
val (value, factors) = PalindromeProducts(1000, 9999).smallest
value should be (1002001)
factors should be (Set((1001, 1001)))
PalindromeProducts(10000, 1).smallest should be(None)
}

it should "find largest palindrome from a four digit factors" in {
test("error result for largest if min is more than max") {
pending
val (value, factors) = PalindromeProducts(1000, 9999).largest
value should be (99000099)
factors should be (Set((9901, 9999)))
PalindromeProducts(2, 1).largest should be(None)
}
}
50 changes: 50 additions & 0 deletions testgen/src/main/scala/PalindromeProductsTestGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.io.File

import testgen.TestSuiteBuilder.{toString, _}
import testgen._

object PalindromeProductsTestGenerator {
def main(args: Array[String]): Unit = {
val file = new File("src/main/resources/palindrome-products.json")

def toStringFromMap(m: Map[String, Any]): String =
s"Some((${m("value").toString}, " +
s"Set(${m("factors").asInstanceOf[List[List[Int]]].map(xs => "(" + xs.map(_.toString).mkString(", ") + ")").mkString(", ")})))"


def toString(expected: CanonicalDataParser.Expected): String = {
expected match {
case Left(_) => "None"
case Right(m: Map[String, any]) => toStringFromMap(m)
}
}

def fromLabeledTest(argNames: String*): ToTestCaseData =
withLabeledTest { sut =>
labeledTest =>
val args = sutArgs(labeledTest.result, argNames: _*)
val property = labeledTest.property
val sutCall = s"""$sut($args).$property"""
val expected = toString(labeledTest.expected)
TestCaseData(labeledTest.description, sutCall, expected)
}

val code =
TestSuiteBuilder.build(file, fromLabeledTest("input_min", "input_max"),
Seq(),
Seq("// 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."))

println(s"-------------")
println(code)
println(s"-------------")
}
}