Created
December 12, 2014 13:01
-
-
Save sungkmi/17d417a2c60689522adc 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 FileFixIt extends App { | |
def subsetdir(input: String): Set[String] = | |
(input split '/' drop 1).foldLeft(("", Set.empty[String])) { | |
case ((current, acc), dir) => | |
val d = s"$current/$dir" | |
(d, acc + d) | |
}._2 | |
def numOfMkdir(exists: Set[String], todo: Set[String]): Int = | |
((todo flatMap subsetdir) -- (exists flatMap subsetdir)).size | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
def readFiles(n: Int) = Seq.fill(n)(lineIn.next()).toSet | |
val Array(n, m) = lineIn.next().split(' ').map(_.toInt) | |
val exists = readFiles(n) | |
val todo = readFiles(m) | |
lineOut(s"Case #$i: ${numOfMkdir(exists, todo)}") | |
} | |
val writer = new java.io.PrintWriter("a.large.out") | |
try { | |
process(io.Source.fromFile("A-large-practice.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 FileFixIt._ | |
class FileFixItTest extends FunSuite { | |
test("sample #1") { | |
assert(numOfMkdir(Set.empty, Set("/home/gcj/finals", "/home/gcj/quals")) === 4) | |
} | |
test("sample #2") { | |
assert(numOfMkdir(Set("/chicken", "/chicken/egg"), Set("/chicken")) === 0) | |
} | |
test("sample #3") { | |
assert(numOfMkdir(Set("/a"), Set("/a/b", "/a/c", "/b/b")) === 4) | |
} | |
test("sample case") { | |
val input = """3 | |
0 2 | |
/home/gcj/finals | |
/home/gcj/quals | |
2 1 | |
/chicken | |
/chicken/egg | |
/chicken | |
1 3 | |
/a | |
/a/b | |
/a/c | |
/b/b""".lines | |
val expected = """Case #1: 4 | |
Case #2: 0 | |
Case #3: 4""".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.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.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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment