Skip to content

Instantly share code, notes, and snippets.

@dbuschman7
Created May 23, 2016 22:44
Show Gist options
  • Save dbuschman7/ffc924263808e673ceb4d6a76c457504 to your computer and use it in GitHub Desktop.
Save dbuschman7/ffc924263808e673ceb4d6a76c457504 to your computer and use it in GitHub Desktop.
package me.lightspeed7.dsug.streams
import org.scalatest.FunSuite
import org.scalatest.Matchers._
class NodeSolutionTest extends FunSuite {
def function(in: Seq[String]): Map[String, Int] = {
in.flatMap(_.split(" "))
.groupBy(identity)
.map { case (k, occ) => (k, occ.size) }
}
def parse(in: String) = Option(in).map(_.split("\n").toSeq).getOrElse(Seq())
// Tests
test("noDegreesForEmptyInput") {
function(parse(null)).size should be(0)
}
test("twoDegreesOf1forOneEdge") {
val result = function(parse("1 2"))
result.size should be(2)
result.toSet should equal(Set(("1", 1), ("2", 1)))
}
test("incomingAndOutgoringEdgeIsCounted") {
val result = function(Seq("1 2", "2 3"))
result.size should be(3)
result.toSeq.sorted.toSet should be(Set(("1", 1), ("2", 2), ("3", 1)))
}
test("example set of 13 nodes") {
val input = Seq("1 2", "1 3", "2 3", "1 4", "3 4", "1 5", "2 5", "1 6", "2 6", "3 6", "3 7",
"5 7", "6 7", "3 8", "4 8", "6 8", "7 8", "2 9", "5 9", "6 9", "2 10", "9 10", "6 11", "7 11", "8 11",
"9 11", "10 11", "1 12", "6 12", "7 12", "8 12", "11 12", "6 13", "7 13", "9 13", "10 13", "11 13",
"5 14", "8 14", "12 14", "13 14", "1 15", "2 15", "5 15", "9 15", "10 15", "11 15", "12 15", "13 15",
"1 16", "2 16", "5 16", "6 16", "11 16", "12 16", "13 16", "14 16", "15 16")
val result = function(input)
result.size should be(16)
val answers = Seq(8, 8, 6, 3, 7, 10, 7, 7, 7, 5, 9, 8, 8, 5, 9, 9)
answers.size should be(16)
result.toSeq.sortBy(_._1.toInt)
.zip(answers).map {
case ((node, count), answer) =>
(count - answer) match {
case 0 => // all good, number match
case _ => println(s"Count for node ${node} was ${count}, expected ${answer}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment