Skip to content

Instantly share code, notes, and snippets.

@ByeongjunYu
Last active April 1, 2016 13:05
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 ByeongjunYu/abc2e3c2e5aea4be7e8a96e0b3261aaa to your computer and use it in GitHub Desktop.
Save ByeongjunYu/abc2e3c2e5aea4be7e8a96e0b3261aaa to your computer and use it in GitHub Desktop.
import scala.annotation.tailrec
val input = IndexedSeq(9, 9, 12, 12, 12, 15, 16, 20)
def run(input: IndexedSeq[Int]): IndexedSeq[Int] = {
val result = IndexedSeq.empty[Int]
go(input: IndexedSeq[Int], result: IndexedSeq[Int])
}
@tailrec
def go(input: IndexedSeq[Int], result: IndexedSeq[Int]): IndexedSeq[Int] = {
if(input.isEmpty) {
result
} else {
val salePrice = input.head
val newList = removeRegularPrice(input)
go(newList, result ++ IndexedSeq(salePrice))
}
}
def removeRegularPrice(prices: IndexedSeq[Int]) : IndexedSeq[Int] = {
if(prices.nonEmpty) {
val salePrice = prices.head
val regPrice = salePrice * 4 / 3
val index = prices.indexOf(regPrice)
val (fst, snd) = prices.splitAt(index)
fst.tail ++ snd.tail
} else {
IndexedSeq.empty[Int]
}
}
run(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment