Skip to content

Instantly share code, notes, and snippets.

@soursop
Created April 1, 2016 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soursop/db548ffbca5d3307301edfdd0f59f624 to your computer and use it in GitHub Desktop.
Save soursop/db548ffbca5d3307301edfdd0f59f624 to your computer and use it in GitHub Desktop.
object CandysJams {
def main(args: Array[String]) {
run()
}
def solve(size: Int, seq: Seq[Int], result: Seq[Int] = Seq()): Seq[Int] = {
if (size < 1) result
else {
val originPrice: Int = (seq.head / 3).toInt * 4
val (left, right) = seq.tail.span(_ <= originPrice)
left match {
case Nil => solve(size - 1, right, result :+ seq.head)
case _ => solve(size - 1, left.init ++ right, result :+ seq.head)
}
}
}
def test() {
val input = """2
3
15 20 60 75 80 100
4
9 9 12 12 12 15 16 20""".lines
val expected = """Case #1: 15 60 75
Case #2: 9 9 12 15""".lines
def lineComparison(input: Iterator[String], expected: Iterator[String]) {
process(input) { s =>
for (line <- s.lines) println(line.trim == expected.next().trim)
}
println(expected.hasNext == false)
}
lineComparison(input, expected)
}
def run() {
val writer = new java.io.PrintWriter("a-large.out")
try {
process(io.Source.fromFile("A-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 size = lineIn.next().toInt
val seq = lineIn.next() split ' ' map (_.toInt)
val answer = solve(size, seq)
lineOut(s"Case #$i: ${answer.mkString(" ")}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment