<commit_before># -*- coding: utf-8 -*-
from copy import deepcopy

POTTER_VOL_1 = 1
POTTER_VOL_2 = 2
POTTER_VOL_3 = 3
POTTER_VOL_4 = 4
POTTER_VOL_5 = 5

BOOK_PRICE = 100


class Cart(object):

    def __init__(self):
        self._books = {}

    def add_book(self, book_id):
        self._books[book_id] = self._books.get(book_id, 0) + 1

    def get_price(self):
        booksets = BookSet.group_books_to_set(self._books)

        price = 0
        for bookset in booksets:
            book_count = bookset.get_book_count()
            discount = _get_bookset_discount(bookset)
            price += BOOK_PRICE * book_count * discount
        return price


class BookSet(object):

    def __init__(self, books):
        assert iter(books)
        self._bookset = set(books)

    def __eq__(self, other):
        if not isinstance(other, BookSet):
            return False
        return self._bookset == set(other.iter_book_id())

    def get_book_count(self):
        return len(self._bookset)

    def iter_book_id(self):
        for book_id in self._bookset:
            yield book_id

    @staticmethod
    def group_books_to_set(books):
        books = deepcopy(books)

        booksets = []
        while sum(books.itervalues(), 0):
            bookset = BookSet(books)
            booksets.append(bookset)
            for book_id in bookset.iter_book_id():
                books[book_id] -= 1
                if books[book_id] == 0:
                    del books[book_id]

        return booksets


def _get_bookset_discount(bookset):
    assert isinstance(bookset, BookSet)

    book_count = bookset.get_book_count()

    if book_count == 2:  # 兩本套書
        return 0.95
    elif book_count == 3:  # 三本套書
        return 0.9
    elif book_count == 4:  # 四本套書
        return 0.8
    elif book_count == 5:  # 五本套書
        return 0.75
    else:  # 非套書
        return 1.0<commit_msg>重構 : 將計算 discount price 的邏輯，移到 BookSet.get_discount_price()<commit_after># -*- coding: utf-8 -*-
from copy import deepcopy

POTTER_VOL_1 = 1
POTTER_VOL_2 = 2
POTTER_VOL_3 = 3
POTTER_VOL_4 = 4
POTTER_VOL_5 = 5

BOOK_PRICE = 100


class Cart(object):

    def __init__(self):
        self._books = {}

    def add_book(self, book_id):
        self._books[book_id] = self._books.get(book_id, 0) + 1

    def get_price(self):
        booksets = BookSet.group_books_to_set(self._books)

        price = 0
        for bookset in booksets:
            price += bookset.get_discount_price()
        return price


class BookSet(object):

    def __init__(self, books):
        assert iter(books)
        self._bookset = set(books)

    def __eq__(self, other):
        if not isinstance(other, BookSet):
            return False
        return self._bookset == set(other.iter_book_id())

    def _get_book_count(self):
        return len(self._bookset)

    def iter_book_id(self):
        for book_id in self._bookset:
            yield book_id

    def get_discount_price(self):
        book_count = self._get_book_count()
        discount = self._get_discount()
        return BOOK_PRICE * book_count * discount

    @staticmethod
    def group_books_to_set(books):
        books = deepcopy(books)

        booksets = []
        while sum(books.itervalues(), 0):
            bookset = BookSet(books)
            booksets.append(bookset)
            for book_id in bookset.iter_book_id():
                books[book_id] -= 1
                if books[book_id] == 0:
                    del books[book_id]

        return booksets

    def _get_discount(self):
        book_count = self._get_book_count()

        if book_count == 2:  # 兩本套書
            return 0.95
        elif book_count == 3:  # 三本套書
            return 0.9
        elif book_count == 4:  # 四本套書
            return 0.8
        elif book_count == 5:  # 五本套書
            return 0.75
        else:  # 非套書
            return 1.0
{'fraction_non_alphanumeric': 0.0922242314647378, 'fraction_numerical': 0.018599845001291655, 'mean_word_length': 2.015576323987539, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '886278', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1309, 'n_words': 293}