forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
29 lines (19 loc) · 706 Bytes
/
example.py
File metadata and controls
29 lines (19 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
BOOK_PRICE = 8
def _group_price(size):
discounts = [0, .05, .1, .2, .25]
if not (0 < size <= 5):
raise ValueError('size must be in 1..' + len(discounts))
return BOOK_PRICE * size * (1 - discounts[size - 1])
def calculate_total(books, price_so_far=0.):
if not books:
return price_so_far
groups = list(set(books))
min_price = float('inf')
for i in range(len(groups)):
remaining_books = books[:]
for v in groups[:i + 1]:
remaining_books.remove(v)
price = calculate_total(remaining_books,
price_so_far + _group_price(i + 1))
min_price = min(min_price, price)
return min_price