exercism-solutions/python/book-store/book_store.py
2021-06-28 19:12:56 +03:00

33 lines
805 B
Python

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()))