Last active
April 1, 2016 13:05
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
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