forked from exercism/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create SaddlePointsTestGenerator. Update SaddlePointsTest. Refs exerc…
- Loading branch information
Showing
3 changed files
with
67 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
case class Matrix(values: List[List[Int]]) { | ||
lazy val saddlePoints: Set[(Int, Int)] = { | ||
val rowMaxes = values.map(_.max).toVector | ||
val columns = values.transpose | ||
val colMins = columns.map(_.min).toVector | ||
(for { | ||
i <- 0 until values.length | ||
j <- 0 until columns.length | ||
if rowMaxes(i) == colMins(j) | ||
} yield (i, j)).toSet | ||
if (values.isEmpty || columns.isEmpty) { | ||
Set() | ||
} else { | ||
val rowMaxes = values.map(_.max).toVector | ||
val colMins = columns.map(_.min).toVector | ||
(for { | ||
i <- values.indices | ||
j <- columns.indices | ||
if rowMaxes(i) == colMins(j) | ||
} yield (i, j)).toSet | ||
} | ||
} | ||
} |
35 changes: 21 additions & 14 deletions
35
exercises/saddle-points/src/test/scala/SaddlePointsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
import org.scalatest.{Matchers, FlatSpec} | ||
import org.scalatest.{Matchers, FunSuite} | ||
|
||
class SaddlePointsSpecs extends FlatSpec with Matchers { | ||
it should "handle one saddle" in { | ||
val points = Matrix(List(List(9, 8, 7), List(5, 3, 2), List(6, 6, 7))).saddlePoints | ||
points should be (Set((1, 0))) | ||
/** @version 1.0.0 */ | ||
class SaddlePointsTest extends FunSuite with Matchers { | ||
|
||
test("Can identify single saddle point") { | ||
Matrix(List(List(9, 8, 7), List(5, 3, 2), List(6, 6, 7))).saddlePoints should be( | ||
Set((1, 0))) | ||
} | ||
|
||
test("Can identify that empty matrix has no saddle points") { | ||
pending | ||
Matrix(List(List())).saddlePoints should be(Set()) | ||
} | ||
|
||
it should "handle multiple saddles" in { | ||
test("Can identify lack of saddle points when there are none") { | ||
pending | ||
val points = Matrix(List(List(5, 3, 5, 4), List(6, 4, 7, 3), List(5, 1, 5, 3))).saddlePoints | ||
points should be (Set((0, 0), (0, 2), (2, 0), (2, 2))) | ||
Matrix(List(List(1, 2, 3), List(3, 1, 2), List(2, 3, 1))).saddlePoints should be( | ||
Set()) | ||
} | ||
|
||
it should "handle no saddles" in { | ||
test("Can identify multiple saddle points") { | ||
pending | ||
val points = Matrix(List(List(2, 1), List(1, 2))).saddlePoints | ||
points should be (Set()) | ||
Matrix(List(List(4, 5, 4), List(3, 5, 5), List(1, 5, 4))).saddlePoints should be( | ||
Set((0, 1), (1, 1), (2, 1))) | ||
} | ||
|
||
it should "handle empty matrix" in { | ||
test("Can identify saddle point in bottom right corner") { | ||
pending | ||
val points = Matrix(List()).saddlePoints | ||
points should be (Set()) | ||
Matrix(List(List(8, 7, 9), List(6, 7, 6), List(3, 2, 5))).saddlePoints should be( | ||
Set((2, 2))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.io.File | ||
|
||
import testgen.TestSuiteBuilder._ | ||
import testgen._ | ||
|
||
object SaddlePointsTestGenerator { | ||
def toString(expected: CanonicalDataParser.Expected): String = { | ||
expected match { | ||
case Right(xs: List[Map[String, Int]]) => | ||
val tuples = xs.map(m => s"(${m("row")}, ${m("column")})").mkString(", ") | ||
s"""Set($tuples)""" | ||
case _ => throw new IllegalArgumentException | ||
} | ||
} | ||
|
||
def fromLabeledTest(argNames: String*): ToTestCaseData = | ||
withLabeledTest { sut => | ||
labeledTest => | ||
val args = sutArgs(labeledTest.result, argNames: _*) | ||
val property = labeledTest.property | ||
val sutCall = | ||
s"""Matrix($args).$property""" | ||
val expected = toString(labeledTest.expected) | ||
TestCaseData(labeledTest.description, sutCall, expected) | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
val file = new File("src/main/resources/saddle-points.json") | ||
|
||
val code = TestSuiteBuilder.build(file, fromLabeledTest("input")) | ||
println(s"-------------") | ||
println(code) | ||
println(s"-------------") | ||
} | ||
} |