Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "book-store",
"name": "Book Store",
"uuid": "590c6355-dbb4-44a0-a2d7-98490055c7e9",
"practices": [],
"prerequisites": [],
"difficulty": 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you say this is an easy exercise? The "global" average across tracks is 6.4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this a tricky one, let's make this a 5, shall we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still waiting for this change.

Copy link
Contributor

@rmonnet rmonnet Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the solution provided here. It is different from the one I built for other tracks but it passes the tests. This solution is way more compact than mine (which is good) but I am having problem following the logic. I think this is definitively a difficult problem (at least for me(, the instructions make it look easy but it isn't. I would vote for a 9 but would be okay if it is at least a 5.

Copy link
Contributor Author

@hucancode hucancode Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmonnet
My solution based on an observation for this particular 5 book series problem:

  • There is a steep discount from 3 series to 4 series, 10% to 20%, no other tier jump offer this much benefit
  • In case where we can build 5-3 set to get discount of 25% on 5 and 10% on 3, we can and should always rearrange them to 4-4 set to get discount of 20% on 8 books. We get better discount without any catch, 0.25 * 5 + 0.1 * 3 = 1.55 < 0.2 * 8 = 1.6

The rest are just implementation detail. I count book count for each series, sort them, so that we buy good value set first. Then I try to upgrade 3-set to 4-set for the cost of downgrading 5-set to 4-set, which is a net gain as we observed earlier.

},
{
"slug": "change",
"name": "Change",
Expand Down
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.
19 changes: 19 additions & 0 deletions exercises/practice/book-store/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"hucancode"
],
"files": {
"solution": [
"book_store.odin"
],
"test": [
"book_store_test.odin"
],
"example": [
".meta/example.odin"
]
},
"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"
}
41 changes: 41 additions & 0 deletions exercises/practice/book-store/.meta/example.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package book_store

import "core:slice"

calculate :: proc(counts: [5]u32) -> u32 {
ret: f32 = 0.0
used: u32 = 0
base_price: f32 = 800.0
n := len(counts)
for i in 0 ..< n {
m := n - i
x := counts[i] - used
used = counts[i]
price := base_price * f32(m) * f32(x)
discount: f32
switch m {
case 2:
discount = 0.95
case 3:
discount = 0.90
case 4:
discount = 0.80
case 5:
discount = 0.75
case:
discount = 1.0
}
ret += price * discount
}
return u32(ret)
}

total :: proc(basket: []u32) -> u32 {
counts: [5]u32
for book in basket do counts[book - 1] += 1
slice.sort(counts[:])
delta := min(counts[0], counts[2] - counts[1])
counts[0] -= delta
counts[1] += delta
return calculate(counts)
}
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"
6 changes: 6 additions & 0 deletions exercises/practice/book-store/book_store.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package book_store

total :: proc() -> string {
// Implement this procedure.
return ""
}
153 changes: 153 additions & 0 deletions exercises/practice/book-store/book_store_test.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package book_store

import "core:testing"

@(test)
test_only_a_single_book :: proc(t: ^testing.T) {
basket := [?]u32{1}
result := total(basket[:])
expected: u32 = 800
testing.expect_value(t, result, expected)
}

@(test)
test_two_of_the_same_book :: proc(t: ^testing.T) {
basket := [?]u32{2, 2}
result := total(basket[:])
expected: u32 = 1600
testing.expect_value(t, result, expected)
}

@(test)
test_empty_basket :: proc(t: ^testing.T) {
basket := [?]u32{}
result := total(basket[:])
expected: u32 = 0
testing.expect_value(t, result, expected)
}

@(test)
test_two_different_books :: proc(t: ^testing.T) {
basket := [?]u32{1, 2}
result := total(basket[:])
expected: u32 = 1520
testing.expect_value(t, result, expected)
}

@(test)
test_three_different_books :: proc(t: ^testing.T) {
basket := [?]u32{1, 2, 3}
result := total(basket[:])
expected: u32 = 2160
testing.expect_value(t, result, expected)
}

@(test)
test_four_different_books :: proc(t: ^testing.T) {
basket := [?]u32{1, 2, 3, 4}
result := total(basket[:])
expected: u32 = 2560
testing.expect_value(t, result, expected)
}

@(test)
test_five_different_books :: proc(t: ^testing.T) {
basket := [?]u32{1, 2, 3, 4, 5}
result := total(basket[:])
expected: u32 = 3000
testing.expect_value(t, result, expected)
}

@(test)
test_two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 2, 3, 3, 4, 5}
result := total(basket[:])
expected: u32 = 5120
testing.expect_value(t, result, expected)
}

@(test)
test_two_groups_of_four_is_cheaper_than_groups_of_five_and_three :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 3, 4, 4, 5, 5}
result := total(basket[:])
expected: u32 = 5120
testing.expect_value(t, result, expected)
}

@(test)
test_group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 2, 3, 4}
result := total(basket[:])
expected: u32 = 4080
testing.expect_value(t, result, expected)
}

@(test)
test_two_each_of_first_four_books_and_one_copy_each_of_rest :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 2, 3, 3, 4, 4, 5}
result := total(basket[:])
expected: u32 = 5560
testing.expect_value(t, result, expected)
}

@(test)
test_two_copies_of_each_book :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}
result := total(basket[:])
expected: u32 = 6000
testing.expect_value(t, result, expected)
}

@(test)
test_three_copies_of_first_book_and_two_each_of_remaining :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1}
result := total(basket[:])
expected: u32 = 6800
testing.expect_value(t, result, expected)
}

@(test)
test_three_each_of_first_two_books_and_two_each_of_remaining_books :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2}
result := total(basket[:])
expected: u32 = 7520
testing.expect_value(t, result, expected)
}

@(test)
test_four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three :: proc(
t: ^testing.T,
) {
basket := [?]u32{1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5}
result := total(basket[:])
expected: u32 = 10240
testing.expect_value(t, result, expected)
}

@(test)
test_check_that_groups_of_four_are_created_properly_even_when_there_are_more_groups_of_three_than_groups_of_five :: proc(
t: ^testing.T,
) {
basket := [?]u32{1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5}
result := total(basket[:])
expected: u32 = 14560
testing.expect_value(t, result, expected)
}

@(test)
test_one_group_of_one_and_four_is_cheaper_than_one_group_of_two_and_three :: proc(t: ^testing.T) {
basket := [?]u32{1, 1, 2, 3, 4}
result := total(basket[:])
expected: u32 = 3360
testing.expect_value(t, result, expected)
}

@(test)
test_one_group_of_one_and_two_plus_three_groups_of_four_is_cheaper_than_one_group_of_each_size :: proc(
t: ^testing.T,
) {
basket := [?]u32{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5}
result := total(basket[:])
expected: u32 = 10000
testing.expect_value(t, result, expected)
}
Loading