Python: book_store
This commit is contained in:
parent
8190c11851
commit
f5e841e110
4 changed files with 232 additions and 0 deletions
33
python/book-store/book_store.py
Normal file
33
python/book-store/book_store.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from collections import Counter
|
||||
from itertools import combinations
|
||||
|
||||
DISCOUNTS = [0, 0, 5, 10, 20, 25]
|
||||
BOOK_COST = 8
|
||||
|
||||
|
||||
def get_discount(counter):
|
||||
|
||||
xs = sorted([i for i in counter if i != 0], reverse=True)
|
||||
|
||||
if not xs:
|
||||
return 0
|
||||
|
||||
discount = 0
|
||||
|
||||
for group_size in range(1, len(xs) + 1):
|
||||
for xs1 in combinations(xs, len(xs)):
|
||||
xs1 = [item - 1 if index < group_size else item
|
||||
for index, item in enumerate(xs1)]
|
||||
curr_discount = (
|
||||
DISCOUNTS[group_size]*group_size + get_discount(xs1)
|
||||
)
|
||||
if curr_discount > discount:
|
||||
discount = curr_discount
|
||||
|
||||
return discount
|
||||
|
||||
|
||||
def total(basket):
|
||||
|
||||
return BOOK_COST*(len(basket)*100
|
||||
- get_discount(Counter(basket).values()))
|
||||
Loading…
Add table
Add a link
Reference in a new issue