Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created September 4, 2015 12:08
Show Gist options
  • Save sungkmi/a9fdc87edc823239bfe8 to your computer and use it in GitHub Desktop.
Save sungkmi/a9fdc87edc823239bfe8 to your computer and use it in GitHub Desktop.
object IOError extends App {
def translate = (_: String).grouped(8).map(_.foldLeft(0)(
(acc, bit) => acc << 1 | (bit match {
case 'O' => 0
case 'I' => 1
})
).toChar).mkString
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
lineIn.next()
lineOut(s"Case #$i: ${translate(lineIn.next())}")
}
val filename = "A-small-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()
}
}
import org.scalatest._
import IOError._
class IOErrorTest extends FunSuite {
test("sample #1") {
assert(translate("OIOOIIIIOIOOIOII") === "OK")
}
test("sample #2") {
assert(translate("OIOOIOOIOOIOOOOOOOIOOIIIOOIIIIOOOOIIOOIIOOIOOIIIOOIOOOOOOOIOOOIOOIOOOOIIOOIIOOOOOIIOOIOOOOIIOOIIOOIOOOOOOIOOIOIOOOIIOIOOOIIOIIOIOOIOOOIOOOIOOOOIOOIOOOOOOOIIIOIOOOIOIOOI") === """I '<3' "C0d3 J4m"! :)""")
}
test("sample case") {
val input = """2
2
OIOOIIIIOIOOIOII
21
OIOOIOOIOOIOOOOOOOIOOIIIOOIIIIOOOOIIOOIIOOIOOIIIOOIOOOOOOOIOOOIOOIOOOOIIOOIIOOOOOIIOOIOOOOIIOOIIOOIOOOOOOIOOIOIOOOIIOIOOOIIOIIOIOOIOOOIOOOIOOOOIOOIOOOOOOOIIIOIOOOIOIOOI""".lines
val expected = """Case #1: OK
Case #2: I '<3' "C0d3 J4m"! :)""".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)
}
def lineComparison(input: Iterator[String], expected: Iterator[String]) {
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