Skip to content

Commit 47766ce

Browse files
authored
Sync book-store with problem-specifications (#1742)
1 parent 4383ddd commit 47766ce

5 files changed

Lines changed: 191 additions & 126 deletions

File tree

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
# Instructions
22

3-
To try and encourage more sales of different books from a popular 5 book
4-
series, a bookshop has decided to offer discounts on multiple book purchases.
3+
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.
54

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

8-
If, however, you buy two different books, you get a 5%
9-
discount on those two books.
7+
If, however, you buy two different books, you get a 5% discount on those two books.
108

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

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

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

17-
Note: that if you buy four books, of which 3 are
18-
different titles, you get a 10% discount on the 3 that
19-
form part of a set, but the fourth book still costs $8.
15+
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.
2016

21-
Your mission is to write a piece of code to calculate the
22-
price of any conceivable shopping basket (containing only
23-
books of the same series), giving as big a discount as
24-
possible.
17+
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.
2518

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

@@ -33,36 +26,36 @@ For example, how much does this basket of books cost?
3326

3427
One way of grouping these 8 books is:
3528

36-
- 1 group of 5 --> 25% discount (1st,2nd,3rd,4th,5th)
37-
- +1 group of 3 --> 10% discount (1st,2nd,3rd)
29+
- 1 group of 5 (1st, 2nd,3rd, 4th, 5th)
30+
- 1 group of 3 (1st, 2nd, 3rd)
3831

3932
This would give a total of:
4033

4134
- 5 books at a 25% discount
42-
- +3 books at a 10% discount
35+
- 3 books at a 10% discount
4336

4437
Resulting in:
4538

46-
- 5 x (8 - 2.00) == 5 x 6.00 == $30.00
47-
- +3 x (8 - 0.80) == 3 x 7.20 == $21.60
39+
- 5 × (100% - 25%) × $8 = 5 × $6.00 = $30.00, plus
40+
- 3 × (100% - 10%) × $8 = 3 × $7.20 = $21.60
4841

49-
For a total of $51.60
42+
Which equals $51.60.
5043

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

53-
- 1 group of 4 books --> 20% discount (1st,2nd,3rd,4th)
54-
- +1 group of 4 books --> 20% discount (1st,2nd,3rd,5th)
46+
- 1 group of 4 books (1st, 2nd, 3rd, 4th)
47+
- 1 group of 4 books (1st, 2nd, 3rd, 5th)
5548

5649
This would give a total of:
5750

5851
- 4 books at a 20% discount
59-
- +4 books at a 20% discount
52+
- 4 books at a 20% discount
6053

6154
Resulting in:
6255

63-
- 4 x (8 - 1.60) == 4 x 6.40 == $25.60
64-
- +4 x (8 - 1.60) == 4 x 6.40 == $25.60
56+
- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60, plus
57+
- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60
6558

66-
For a total of $51.20
59+
Which equals $51.20.
6760

6861
And $51.20 is the price with the biggest discount.

exercises/practice/book-store/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
},
2929
"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.",
3030
"source": "Inspired by the harry potter kata from Cyber-Dojo.",
31-
"source_url": "http://cyber-dojo.org"
31+
"source_url": "https://cyber-dojo.org"
3232
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% for test in cases %}
2+
#[test]
3+
{% if loop.index != 1 -%}
4+
#[ignore]
5+
{% endif -%}
6+
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
7+
let input = &{{ test.input.basket | json_encode() }};
8+
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
9+
let expected = {{ test.expected | json_encode() }};
10+
assert_eq!(output, expected);
11+
}
12+
{% endfor -%}
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[17146bd5-2e80-4557-ab4c-05632b6b0d01]
13+
description = "Only a single book"
14+
15+
[cc2de9ac-ff2a-4efd-b7c7-bfe0f43271ce]
16+
description = "Two of the same book"
17+
18+
[5a86eac0-45d2-46aa-bbf0-266b94393a1a]
19+
description = "Empty basket"
20+
21+
[158bd19a-3db4-4468-ae85-e0638a688990]
22+
description = "Two different books"
23+
24+
[f3833f6b-9332-4a1f-ad98-6c3f8e30e163]
25+
description = "Three different books"
26+
27+
[1951a1db-2fb6-4cd1-a69a-f691b6dd30a2]
28+
description = "Four different books"
29+
30+
[d70f6682-3019-4c3f-aede-83c6a8c647a3]
31+
description = "Five different books"
32+
33+
[78cacb57-911a-45f1-be52-2a5bd428c634]
34+
description = "Two groups of four is cheaper than group of five plus group of three"
35+
36+
[f808b5a4-e01f-4c0d-881f-f7b90d9739da]
37+
description = "Two groups of four is cheaper than groups of five and three"
38+
39+
[fe96401c-5268-4be2-9d9e-19b76478007c]
40+
description = "Group of four plus group of two is cheaper than two groups of three"
41+
42+
[68ea9b78-10ad-420e-a766-836a501d3633]
43+
description = "Two each of first four books and one copy each of rest"
44+
45+
[c0a779d5-a40c-47ae-9828-a340e936b866]
46+
description = "Two copies of each book"
47+
48+
[18fd86fe-08f1-4b68-969b-392b8af20513]
49+
description = "Three copies of first book and two each of remaining"
50+
51+
[0b19a24d-e4cf-4ec8-9db2-8899a41af0da]
52+
description = "Three each of first two books and two each of remaining books"
53+
54+
[bb376344-4fb2-49ab-ab85-e38d8354a58d]
55+
description = "Four groups of four are cheaper than two groups each of five and three"
56+
57+
[5260ddde-2703-4915-b45a-e54dbbac4303]
58+
description = "Check that groups of four are created properly even when there are more groups of three than groups of five"
59+
60+
[b0478278-c551-4747-b0fc-7e0be3158b1f]
61+
description = "One group of one and four is cheaper than one group of two and three"
62+
63+
[cf868453-6484-4ae1-9dfc-f8ee85bbde01]
64+
description = "One group of one and two plus three groups of four is cheaper than one group of each size"

0 commit comments

Comments
 (0)