Created
March 27, 2015 13:19
-
-
Save sungkmi/14fdf810433ff8fd1b17 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 AllYourBase extends App { | |
def minSec(s: String): Long = { | |
val BASE = s.toSet.size max 2 | |
val dict = collection.mutable.Map.empty[Char, Int] | |
(0L /: s) { | |
case (acc, char) => | |
acc * BASE + dict.getOrElseUpdate(char, if (dict.size < 2) dict.size ^ 1 else dict.size) | |
} | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
lineOut(s"Case #$i: ${minSec(lineIn.next())}") | |
} | |
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 AllYourBase._ | |
class AllYourBaseTest extends FunSuite { | |
test("sample #1") { | |
assert(minSec("11001001") === 201) | |
} | |
test("sample #2") { | |
assert(minSec("cats") === 75) | |
} | |
test("sample #3") { | |
assert(minSec("zig") === 11) | |
} | |
test("sample case") { | |
val input = """3 | |
11001001 | |
cats | |
zig""".lines | |
val expected = """Case #1: 201 | |
Case #2: 75 | |
Case #3: 11""".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.ref").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.ref").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