Skip to content

Instantly share code, notes, and snippets.

@superbob
Last active May 25, 2016 07:01
Show Gist options
  • Save superbob/45ef08cf758733635361482207a686af to your computer and use it in GitHub Desktop.
Save superbob/45ef08cf758733635361482207a686af to your computer and use it in GitHub Desktop.
Up-to-date GameOfThrones Kata solution (see https://github.com/ludopradel/DojoGOT). The last test was fixed expect 51,20€ instead of 51,60€
object BookShop {
case class Book(id: Int)
val ONE_BOOK_PRICE = 8
val TWO_DISTINCT_BOOKS_WITH_PROMO_PRICE = 15.2
val THREE_DISTINCT_BOOKS_WITH_PROMO_PRICE = 21.6
val FOUR_DISTINCT_BOOKS_WITH_PROMO_PRICE = 25.6
val FIVE_DISTINCT_BOOKS_WITH_PROMO_PRICE = 30
type Basket[T] = List[List[T]]
val emptyBasket:Basket[Book] = List()
def getPrice(bookList: List[Book]) : Double = {
enumerateBasketCombinations(bookList).map(f => getBasketPrice(f)).sorted.head
}
def enumerateBasketCombinations(bookList: List[Book]): List[Basket[Book]] = {
bookList match {
case List() => List(emptyBasket)
case _ =>
val distinct = bookList.distinct
enumerateListCombinations(distinct).flatMap(oneDistinctList => {
val remainder = bookList diff oneDistinctList
enumerateBasketCombinations(remainder).map(remainderBasketCombinations => oneDistinctList::remainderBasketCombinations)
})
}
}
def enumerateListCombinations(distinct: List[Book]): List[List[Book]] = {
(1 to distinct.length).flatMap(i => distinct.combinations(i).toList).toList
}
def getBasketPrice(basket: Basket[Book]): Double = {
basket.map(l => getDistinctPrice(l)).sum
}
def getDistinctPrice(distinctBookList: List[Book]) : Double = {
distinctBookList.length match {
case 1 => ONE_BOOK_PRICE
case 2 => TWO_DISTINCT_BOOKS_WITH_PROMO_PRICE
case 3 => THREE_DISTINCT_BOOKS_WITH_PROMO_PRICE
case 4 => FOUR_DISTINCT_BOOKS_WITH_PROMO_PRICE
case _ => FIVE_DISTINCT_BOOKS_WITH_PROMO_PRICE
}
}
}
class GameOfThronesTest extends UnitSpec {
"Book Shop" should {
"return 8€ when we want one book" in {
assert(BookShop.getPrice(List(Book(1))) == 8)
}
"return 16€ when we want the same book twice" in {
assert(BookShop.getPrice(List(Book(1), Book(1))) == 16)
}
"return 15,20€ when we want two different books" in {
assert(BookShop.getPrice(List(Book(1), Book(2))) == 15.2)
}
"return 23,20€ when we want three books with the same twice" in {
assert(BookShop.getPrice(List(Book(1), Book(1), Book(3))) == 23.2)
}
"return 21,60€ when we want three different books" in {
assert(BookShop.getPrice(List(Book(1), Book(2), Book(3))) == 21.6)
}
"return 43,20€ when we want three different books twice" in {
assert(BookShop.getPrice(List(Book(1), Book(2), Book(3), Book(1), Book(2), Book(3))) == 43.2)
}
"return 25,60€ when we want four different books" in {
assert(BookShop.getPrice(List(Book(1), Book(2), Book(3), Book(4))) == 25.6)
}
"return 30€ when we want five different books" in {
assert(BookShop.getPrice(List(Book(1), Book(2), Book(3), Book(4), Book(5))) == 30)
}
"return 51,20€ when we want 1*2, 2*2, 3*2, 4 and 5" in {
assert(BookShop.getPrice(List(Book(1), Book(1), Book(2), Book(2), Book(3), Book(3), Book(4), Book(5))) == 51.2)
}
}
}
@OlivierMary
Copy link

"return 51,60€ when we want 1_2, 2_2, 3*3, 4 and 5" le meilleur prix est 51.2€ et non 51.6€ https://github.com/ludopradel/DojoGOT
Car le meilleur prix est 2 groupes de 4 livres différents.

@superbob
Copy link
Author

Oui, j'ai vu ça après coup ...
Cette solution n'est pas optimum.
Elle correspond à ce qu'on était en train de dire à la fin du coding dojo.
En plus le test est mal décrit, c'est 3*2 au lieu de 3*3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment