Skip to content

Instantly share code, notes, and snippets.

@soursop
Created August 21, 2015 12:44
Show Gist options
  • Save soursop/e70bafb7febd074336f5 to your computer and use it in GitHub Desktop.
Save soursop/e70bafb7febd074336f5 to your computer and use it in GitHub Desktop.
object ReverseTest {
def solve(input: Seq[String]): String = {
def solve(left: Seq[String], right: Seq[String], result: Seq[String]): Seq[String] = {
(left.isEmpty, right.isEmpty) match {
case (false, true) => solve(left.init, Nil, result :+ left.last)
case (true, false) => solve(Nil, right.tail, right.head +: result)
case (false, false) => solve(left.init, right.tail, right.head +: result :+ left.last)
case _ => result
}
}
val (left, right) = input splitAt (input.size / 2)
solve(left, right, Seq()).mkString(" ");
}
def main(args: Array[String]) {
run()
}
def run() {
val writer = new java.io.PrintWriter("b-large.out")
try {
process(io.Source.fromFile("B-large-practice.in").getLines)(writer.println)
} finally {
writer.flush()
writer.close()
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val seq = lineIn.next() split ' '
val answer = solve(seq)
lineOut(s"Case #$i: $answer")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment