-
-
Save krikit/e5c9b12640a3987a9c0038b1c0b09b39 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 CodysJam extends App { | |
def removeOrigPrice(prices: IndexedSeq[Long], discountedPrice: Long): IndexedSeq[Long] = { | |
val idx = prices.indexOf(discountedPrice * 4 / 3) | |
prices.slice(0, idx) ++ prices.slice(idx + 1, prices.size) | |
} | |
def getDiscounted(prices: IndexedSeq[Long]): IndexedSeq[Long] = { | |
if (prices.isEmpty) IndexedSeq() | |
else Array(prices.head) ++ getDiscounted(removeOrigPrice(prices.tail, prices.head)) | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
lineIn.next() // number of prices | |
val prices = lineIn.next().split(" ").map(_.toLong) | |
lineOut(s"Case #$i: ${getDiscounted(prices).mkString(" ")}") | |
} | |
val filename = "A-small-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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment