Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active September 21, 2015 02:20
Show Gist options
  • Save sungkmi/d1ac8f284201126e8ae2 to your computer and use it in GitHub Desktop.
Save sungkmi/d1ac8f284201126e8ae2 to your computer and use it in GitHub Desktop.
object PowerLevels extends App {
lazy val logMultifactorials = {
def logMultifactorial(n: Int): Double = Stream.from(9000, -n).takeWhile(_ > 0).map(n => math.log10(n)).sum
Stream.from(1).map(n => (n, logMultifactorial(n))) take 9000
}
def scan(d: Int): Option[Int] = logMultifactorials.filter(_._2 + 1 < d).headOption.map(_._1)
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val d = lineIn.next().toInt
lineOut(s"Case #$i: ${scan(d) map ("IT'S OVER 9000" + "!" * _) getOrElse "..."}")
}
val filename = "C-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 PowerLevels._
class PowerLevelsTest extends FunSuite {
test("sample #1") {
assert(scan(1) === None)
}
test("sample #5") {
assert(scan(31683) === Some(1))
}
test("sample #4") {
assert(scan(31682) === Some(2))
}
test("sample #3") {
assert(scan(206) === Some(156))
}
test("sample #2") {
assert(scan(19) === Some(1990))
}
test("d = 5") {
assert(scan(5) === Some(8999))
}
test("d = 3") {
assert(scan(3) === None)
}
test("d = 4") {
assert(scan(4) === None)
}
test("sample case") {
val input = """5
1
19
206
31682
31683""".lines
val expected = """Case #1: ...
Case #2: IT'S OVER 9000!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Case #3: IT'S OVER 9000!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Case #4: IT'S OVER 9000!!
Case #5: IT'S OVER 9000!""".lines
lineComparison(input, expected)
}
test("full small case") {
val input = io.Source.fromFile("C-small-practice.in").getLines()
val expected = io.Source.fromFile("C-small-practice.out").getLines()
lineComparison(input, expected)
}
test("full large case") {
val input = io.Source.fromFile("C-large-practice.in").getLines()
val expected = io.Source.fromFile("C-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