Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created April 1, 2016 12:03
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 sungkmi/0b0fcdbd809d21ecf71e3775aad0f02e to your computer and use it in GitHub Desktop.
Save sungkmi/0b0fcdbd809d21ecf71e3775aad0f02e to your computer and use it in GitHub Desktop.
object CodyJams extends App {
def salePrices(prices: Seq[Int]): Seq[Int] =
((List.empty[Int], collection.immutable.Queue.empty[Int]) /: prices) {
case ((acc, originals), price) if originals.headOption == Some(price) =>
(acc, originals.tail)
case ((acc, originals), price) =>
(price::acc, originals :+ (price / 3 * 4))
}._1.reverse
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val n = lineIn.next().toInt
val prices = lineIn.next().split(' ').map(_.toInt)
lineOut(s"Case #$i: ${salePrices(prices) mkString " "}")
}
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()
}
}
import org.scalatest._
import CodyJams._
class CodyJamsTest extends FunSuite with Matchers {
test("sample #1") {
assert(salePrices(Seq(15, 20, 60, 75, 80, 100)) === Seq(15, 60, 75))
}
test("sample #2") {
assert(salePrices(Seq(9, 9, 12, 12, 12, 15, 16, 20)) === Seq(9, 9, 12, 15))
}
test("small #6") {
assert(
salePrices(
Seq(270624780, 336144033, 360833040, 448192044, 736130808, 745857189, 981507744, 994476252)) ===
Seq(270624780, 336144033, 736130808, 745857189))
}
test("sample case") {
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
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").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").getLines()
lineComparison(input, expected)
}
def lineComparison(input: Iterator[String], expected: Iterator[String]): Unit = {
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