Skip to content

Commit

Permalink
add Book Store exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
tofische committed May 5, 2024
1 parent 3aab799 commit aaf5324
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,15 @@
"maybe"
]
},
{
"slug": "book-store",
"name": "Book Store",
"uuid": "78971281-153d-4bdc-8f90-4b7eded2b201",
"practices": [],
"prerequisites": [],
"difficulty": 8,
"topics": []
},
{
"slug": "pov",
"name": "POV",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/book-store/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hints

For the sake of simplicity the prices are given as integer amounts in cents.
61 changes: 61 additions & 0 deletions exercises/practice/book-store/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Instructions

To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts on multiple book purchases.

One copy of any of the five books costs $8.

If, however, you buy two different books, you get a 5% discount on those two books.

If you buy 3 different books, you get a 10% discount.

If you buy 4 different books, you get a 20% discount.

If you buy all 5, you get a 25% discount.

Note that if you buy four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs $8.

Your mission is to write code to calculate the price of any conceivable shopping basket (containing only books of the same series), giving as big a discount as possible.

For example, how much does this basket of books cost?

- 2 copies of the first book
- 2 copies of the second book
- 2 copies of the third book
- 1 copy of the fourth book
- 1 copy of the fifth book

One way of grouping these 8 books is:

- 1 group of 5 (1st, 2nd,3rd, 4th, 5th)
- 1 group of 3 (1st, 2nd, 3rd)

This would give a total of:

- 5 books at a 25% discount
- 3 books at a 10% discount

Resulting in:

- 5 × (100% - 25%) × $8 = 5 × $6.00 = $30.00, plus
- 3 × (100% - 10%) × $8 = 3 × $7.20 = $21.60

Which equals $51.60.

However, a different way to group these 8 books is:

- 1 group of 4 books (1st, 2nd, 3rd, 4th)
- 1 group of 4 books (1st, 2nd, 3rd, 5th)

This would give a total of:

- 4 books at a 20% discount
- 4 books at a 20% discount

Resulting in:

- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60, plus
- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60

Which equals $51.20.

And $51.20 is the price with the biggest discount.
23 changes: 23 additions & 0 deletions exercises/practice/book-store/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"tofische"
],
"files": {
"solution": [
"src/BookStore.hs",
"package.yaml"
],
"test": [
"test/Tests.hs"
],
"example": [
".meta/examples/success-standard/src/BookStore.hs"
],
"invalidator": [
"stack.yaml"
]
},
"blurb": "To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts of multiple-book purchases.",
"source": "Inspired by the harry potter kata from Cyber-Dojo.",
"source_url": "https://cyber-dojo.org"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: book-store

dependencies:
- base

library:
exposed-modules: BookStore
source-dirs: src

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- book-store
- hspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module BookStore (total, Book(..)) where

import Data.List ( group, sort )

data Book = First | Second | Third | Fourth | Fifth deriving (Eq, Ord, Show)

-- Reverse sort on count of each book first. I'm not sure if this work in every possible case.
-- Naive algorithm (generate solution candidates by forming groups) is too slow.

total :: [Book] -> Int
total basket = minPrice 5 $ reverse $ sort $ map length $ group $ sort basket
-- Reverse sort the numbers of each books

-- Find min price with a grouping of at most the given size for the given book counts
minPrice :: Int -> [Int] -> Int
minPrice _ [] = 0
minPrice 1 counts = sum $ map (\x -> price 1 * x) counts
minPrice n counts =
if length counts < n
then minPrice (n-1) counts
else min (price n + minPrice n remainingCounts) (minPrice (n-1) counts)
where
remainingCounts = filter (/= 0) (map (\x -> x-1) $ take n counts) <> drop n counts

-- Get the price of a group containing given amount of books
price :: Int -> Int
price 1 = 800 -- 0% discount
price 2 = 1520 -- 5% discount
price 3 = 2160 -- 10% discount
price 4 = 2560 -- 20% discount
price 5 = 3000 -- 25% discount
price _ = 0
64 changes: 64 additions & 0 deletions exercises/practice/book-store/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[17146bd5-2e80-4557-ab4c-05632b6b0d01]
description = "Only a single book"

[cc2de9ac-ff2a-4efd-b7c7-bfe0f43271ce]
description = "Two of the same book"

[5a86eac0-45d2-46aa-bbf0-266b94393a1a]
description = "Empty basket"

[158bd19a-3db4-4468-ae85-e0638a688990]
description = "Two different books"

[f3833f6b-9332-4a1f-ad98-6c3f8e30e163]
description = "Three different books"

[1951a1db-2fb6-4cd1-a69a-f691b6dd30a2]
description = "Four different books"

[d70f6682-3019-4c3f-aede-83c6a8c647a3]
description = "Five different books"

[78cacb57-911a-45f1-be52-2a5bd428c634]
description = "Two groups of four is cheaper than group of five plus group of three"

[f808b5a4-e01f-4c0d-881f-f7b90d9739da]
description = "Two groups of four is cheaper than groups of five and three"

[fe96401c-5268-4be2-9d9e-19b76478007c]
description = "Group of four plus group of two is cheaper than two groups of three"

[68ea9b78-10ad-420e-a766-836a501d3633]
description = "Two each of first four books and one copy each of rest"

[c0a779d5-a40c-47ae-9828-a340e936b866]
description = "Two copies of each book"

[18fd86fe-08f1-4b68-969b-392b8af20513]
description = "Three copies of first book and two each of remaining"

[0b19a24d-e4cf-4ec8-9db2-8899a41af0da]
description = "Three each of first two books and two each of remaining books"

[bb376344-4fb2-49ab-ab85-e38d8354a58d]
description = "Four groups of four are cheaper than two groups each of five and three"

[5260ddde-2703-4915-b45a-e54dbbac4303]
description = "Check that groups of four are created properly even when there are more groups of three than groups of five"

[b0478278-c551-4747-b0fc-7e0be3158b1f]
description = "One group of one and four is cheaper than one group of two and three"

[cf868453-6484-4ae1-9dfc-f8ee85bbde01]
description = "One group of one and two plus three groups of four is cheaper than one group of each size"
21 changes: 21 additions & 0 deletions exercises/practice/book-store/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: book-store
version: 1.0.0.0

dependencies:
- base

library:
exposed-modules: BookStore
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- book-store
- hspec
6 changes: 6 additions & 0 deletions exercises/practice/book-store/src/BookStore.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module BookStore (total, Book(..)) where

data Book = First | Second | Third | Fourth | Fifth

total :: [Book] -> Int
total basket = error "You need to implement this function."
1 change: 1 addition & 0 deletions exercises/practice/book-store/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: lts-20.18
114 changes: 114 additions & 0 deletions exercises/practice/book-store/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFailFast, defaultConfig, hspecWith)

import BookStore (total, Book(..))

main :: IO ()
main = hspecWith defaultConfig {configFailFast = True} specs

specs :: Spec
specs = describe "total" $ for_ cases test
where
test Case{..} = it description $ total basket `shouldBe` expected

data Case = Case { description :: String
, comment :: String
, basket :: [Book]
, expected :: Int
}

cases :: [Case]
cases = [ Case { description = "Only a single book"
, comment = "Suggested grouping, [[1]]."
, basket = [First]
, expected = 800
}
, Case { description = "Two of the same book"
, comment = "Suggested grouping, [[2],[2]]."
, basket = [Second, Second]
, expected = 1600
}
, Case { description = "Empty basket"
, comment = "Suggested grouping, []."
, basket = []
, expected = 0
}
, Case { description = "Two different books"
, comment = "Suggested grouping, [[1,2]]."
, basket = [First, Second]
, expected = 1520
}
, Case { description = "Three different books"
, comment = "Suggested grouping, [[1,2,3]]."
, basket = [First, Second, Third]
, expected = 2160
}
, Case { description = "Four different books"
, comment = "Suggested grouping, [[1,2,3,4]]."
, basket = [First, Second, Third, Fourth]
, expected = 2560
}
, Case { description = "Five different books"
, comment = "Suggested grouping, [[1,2,3,4,5]]."
, basket = [First, Second, Third, Fourth, Fifth]
, expected = 3000
}
, Case { description = "Two groups of four is cheaper than group of five plus group of three"
, comment = "Suggested grouping, [[1,2,3,4],[1,2,3,5]]."
, basket = [First, First, Second, Second, Third, Third, Fourth, Fifth]
, expected = 5120
}
, Case { description = "Two groups of four is cheaper than groups of five and three"
, comment = "Suggested grouping, [[1,2,4,5],[1,3,4,5]]. This differs from the other 'two groups of four' test in that it will fail for solutions that add books to groups in the order in which they appear in the list."
, basket = [First, First, Second, Third, Fourth, Fourth, Fifth, Fifth]
, expected = 5120
}
, Case { description = "Group of four plus group of two is cheaper than two groups of three"
, comment = "Suggested grouping, [[1,2,3,4],[1,2]]."
, basket = [First, First, Second, Second, Third, Fourth]
, expected = 4080
}
, Case { description = "Two each of first four books and one copy each of rest"
, comment = "Suggested grouping, [[1,2,3,4,5],[1,2,3,4]]."
, basket = [First, First, Second, Second, Third, Third, Fourth, Fourth, Fifth]
, expected = 5560
}
, Case { description = "Two copies of each book"
, comment = "Suggested grouping, [[1,2,3,4,5],[1,2,3,4,5]]."
, basket = [First, First, Second, Second, Third, Third, Fourth, Fourth, Fifth, Fifth]
, expected = 6000
}
, Case { description = "Three copies of first book and two each of remaining"
, comment = "Suggested grouping, [[1,2,3,4,5],[1,2,3,4,5],[1]]."
, basket = [First, First, Second, Second, Third, Third, Fourth, Fourth, Fifth, Fifth, First]
, expected = 6800
}
, Case { description = "Three each of first two books and two each of remaining books"
, comment = "Suggested grouping, [[1,2,3,4,5],[1,2,3,4,5],[1,2]]."
, basket = [First, First, Second, Second, Third, Third, Fourth, Fourth, Fifth, Fifth, First, Second]
, expected = 7520
}
, Case { description = "Four groups of four are cheaper than two groups each of five and three"
, comment = "Suggested grouping, [[1,2,3,4],[1,2,3,5],[1,2,3,4],[1,2,3,5]]."
, basket = [First, First, Second, Second, Third, Third, Fourth, Fifth, First, First, Second, Second, Third, Third, Fourth, Fifth]
, expected = 10240
}
, Case { description = "Check that groups of four are created properly even when there are more groups of three than groups of five"
, comment = "Suggested grouping, [[1,2,3,4],[1,2,3,5],[1,2,3,4],[1,2,3,5],[1,2,3],[1,2,3]]."
, basket = [First, First, First, First, First, First, Second, Second, Second, Second, Second, Second, Third, Third, Third, Third, Third, Third, Fourth, Fourth, Fifth, Fifth]
, expected = 14560
}
, Case { description = "One group of one and four is cheaper than one group of two and three"
, comment = "Suggested grouping, [[1],[1,2,3,4]]."
, basket = [First, First, Second, Third, Fourth]
, expected = 3360
}
, Case { description = "One group of one and two plus three groups of four is cheaper than one group of each size"
, comment = "Suggested grouping, [[5],[5,4],[5,4,3,2],[5,4,3,2],[5,4,3,1]]."
, basket = [First, Second, Second, Third, Third, Third, Fourth, Fourth, Fourth, Fourth, Fifth, Fifth, Fifth, Fifth, Fifth]
, expected = 10000
}
]

0 comments on commit aaf5324

Please sign in to comment.