Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active December 7, 2015 03:44
Show Gist options
  • Save sungkmi/b3e69908d5ed77e3596a to your computer and use it in GitHub Desktop.
Save sungkmi/b3e69908d5ed77e3596a to your computer and use it in GitHub Desktop.
object GFiles extends App {
def numberOfFiles(status: Seq[(Int, Long)]): Option[Long] = {
val (froms, tos) = status.map {
case (p, k) =>
(if (p < 100) 100 * k / (p + 1) else k - 1, if (p > 0) 100 * k / p else Long.MaxValue)
}.unzip
if (tos.min - 1 == froms.max) Some(tos.min) else None
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val n = lineIn.next().toInt
val status = Seq.fill(n) {
val Array(p, k) = lineIn.next() split ' '
(p.toInt, k.toLong)
}
lineOut(s"Case #$i: ${numberOfFiles(status) getOrElse -1}")
}
val filename = "B-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()
}
}
import org.scalatest._
import GFiles._
class GFilesTest extends FunSuite with Matchers {
test("sample #1") {
assert(numberOfFiles(Seq((20, 1), (100, 5))) === Some(5))
}
/*
0 0
8 2
8 2
17 4
30 7
39 9
69 16
73 17
82 19
91 21
*/
test("sample #2") {
assert(numberOfFiles(Seq((0, 0), (8, 2), (8,2), (17,4),
(30,7), (39, 9), (69, 16), (73, 17), (82, 19), (91, 21))) === Some(23))
}
test("sample case") {
val input = """3
2
20 1
100 5
10
25 241
27 262
43 407
44 413
57 536
64 601
67 637
84 789
95 893
96 903
10
0 0
8 2
8 2
17 4
30 7
39 9
69 16
73 17
82 19
91 21""".lines
val expected = """Case #1: 5
Case #2: -1
Case #3: 23""".lines
lineComparison(input, expected)
}
test("full small case") {
val input = io.Source.fromFile("B-small-practice.in").getLines()
val expected = io.Source.fromFile("B-small-practice.out").getLines()
lineComparison(input, expected)
}
test("full large case") {
val input = io.Source.fromFile("B-large-practice.in").getLines()
val expected = io.Source.fromFile("B-large-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