Created
April 22, 2016 12:38
-
-
Save sungkmi/dde424af4a54e4180fd57a255cbb8253 to your computer and use it in GitHub Desktop.
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
object CountingSheep extends App { | |
def lastNumber(n: Int): Option[Int] = | |
if (n == 0) None | |
else Some { | |
Stream.from(1).scanLeft(Set.empty[Char]){ | |
(acc, i) => acc ++ (n * i).toString().toSet | |
}.zipWithIndex.dropWhile(_._1 != ('0' to '9').toSet).head._2 * n | |
/* | |
@annotation.tailrec | |
def loop(i: Int, acc: Set[Char]): Int = { | |
val acc0 = acc ++ (n * i).toString().toSet | |
if (acc0 == ('0' to '9').toSet) n * i | |
else loop(i + 1, acc0) | |
} | |
loop(1, Set.empty) | |
*/ | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
lineOut(s"Case #$i: ${lastNumber(lineIn.next().toInt) getOrElse "INSOMNIA"}") | |
} | |
val filename = "A-large-practice" | |
val writer = new java.io.PrintWriter(filename + ".out") | |
try { | |
process(io.Source.fromFile(filename + ".in").getLines) { s => | |
writer.println(s); writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
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
import org.scalatest._ | |
import CountingSheep._ | |
class CountingSheepTest extends FunSuite with Matchers { | |
test("sample #1") { | |
assert(lastNumber(0) === None) | |
} | |
test("sample #2") { | |
assert(lastNumber(1) === Some(10)) | |
} | |
test("sample #3") { | |
assert(lastNumber(2) === Some(90)) | |
} | |
test("sample #4") { | |
assert(lastNumber(11) === Some(110)) | |
} | |
test("sample #5") { | |
assert(lastNumber(1692) === Some(5076)) | |
} | |
test("sample case") { | |
val input = """5 | |
0 | |
1 | |
2 | |
11 | |
1692""".lines | |
val expected = """Case #1: INSOMNIA | |
Case #2: 10 | |
Case #3: 90 | |
Case #4: 110 | |
Case #5: 5076""".lines | |
lineComparison(input, expected) | |
} | |
test("full small case") { | |
val input = io.Source.fromFile("A-small-practice.in").getLines() | |
val expected = io.Source.fromFile("A-small-practice.out").getLines() | |
lineComparison(input, expected) | |
} | |
test("full large case") { | |
val input = io.Source.fromFile("A-large-practice.in").getLines() | |
val expected = io.Source.fromFile("A-large-practice.out").getLines() | |
lineComparison(input, expected) | |
} | |
def lineComparison(input: Iterator[String], expected: Iterator[String]): Unit = { | |
process(input) { s => | |
for (line <- s.lines) assert(line.trim === expected.next().trim) | |
} | |
assert(expected.hasNext === false, "Finished too fast.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment