Skip to content

Commit ebfbbf7

Browse files
thomasjpfancmccandless
authored andcommitted
book-store: updates tests to v1.4.0 (#1478)
* book-store: updates tests to v1.4.0 * FIX: Updates example
1 parent 036241a commit ebfbbf7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

exercises/book-store/book_store_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from book_store import calculate_total
44

55

6-
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0
6+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.4.0
77

88
class BookStoreTest(unittest.TestCase):
99
def test_only_a_single_book(self):
@@ -30,6 +30,9 @@ def test_five_different_books(self):
3030
def test_two_groups_of_4_is_cheaper_than_group_of_5_plus_group_of_3(self):
3131
self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 5]), 5120)
3232

33+
def test_two_groups_of_4_is_cheaper_than_groups_of_5_and_3(self):
34+
self.assertEqual(calculate_total([1, 1, 2, 3, 4, 4, 5, 5]), 5120)
35+
3336
def test_group_of_4_plus_group_of_2_is_cheaper_than_2_groups_of_3(self):
3437
self.assertEqual(calculate_total([1, 1, 2, 2, 3, 4]), 4080)
3538

exercises/book-store/example.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import Counter
12
BOOK_PRICE = 800
23

34

@@ -12,12 +13,16 @@ def calculate_total(books, price_so_far=0.):
1213
if not books:
1314
return price_so_far
1415

15-
groups = list(set(books))
16+
reordered_books = []
17+
groups = []
18+
for value, count in Counter(books).most_common():
19+
reordered_books = reordered_books + [value] * count
20+
groups.append(value)
1621
min_price = float('inf')
1722

1823
for i in range(len(groups)):
1924

20-
remaining_books = books[:]
25+
remaining_books = reordered_books[:]
2126

2227
for v in groups[:i + 1]:
2328
remaining_books.remove(v)

0 commit comments

Comments
 (0)