Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created September 19, 2014 13:13
Show Gist options
  • Save sungkmi/6ca3688b81d5082b1528 to your computer and use it in GitHub Desktop.
Save sungkmi/6ca3688b81d5082b1528 to your computer and use it in GitHub Desktop.
object SnapperChain extends App {
def state(n: Int, k: Int): Boolean =
BigInt(k + 1) % (BigInt(2) pow n) == 0
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val Array(n, k) = lineIn.next() split ' ' map (_.toInt)
lineOut(s"Case #$i: ${if (state(n, k)) "ON" else "OFF"}")
}
}
val writer = new java.io.PrintWriter("a.large.out")
try {
process(io.Source.fromFile("A-large-practice.in").getLines)(writer.println)
} finally {
writer.flush(); writer.close()
}
}
import SnapperChain._
class SnapperChainTest extends FunSuite {
test("sample #1") {
assert(state(1,0) === false)
}
test("sample #2") {
assert(state(1,1) === true)
}
test("sample #4") {
assert(state(4,47) === true)
}
test("sample case") {
val input = """4
1 0
1 1
4 0
4 47""".lines
val expected = """Case #1: OFF
Case #2: ON
Case #3: OFF
Case #4: ON""".lines
process(input) { s =>
for (line <- s.lines)
assert(line === expected.next())
}
}
test("full small case") {
val input = io.Source.fromFile("A-small-practice.in").getLines()
val expected = io.Source.fromFile("a.small.out.ref").getLines()
process(input) { s =>
for (line <- s.lines)
assert(line.trim === expected.next().trim)
}
}
test("full large case") {
val input = io.Source.fromFile("A-large-practice.in").getLines()
val expected = io.Source.fromFile("a.large.out.ref").getLines()
process(input) { s =>
for (line <- s.lines)
assert(line.trim === expected.next().trim)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment