File size: 2,135 Bytes
d2213a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Утилиты для работы с игральными костями"""

from typing import Dict, List, Tuple
from collections import defaultdict
import itertools


class DiceCombinations:
    """Предрасчет комбинаций для игральных костей"""

    @staticmethod
    def get_two_dice_combinations() -> Dict[int, int]:
        """
        Получение количества комбинаций для сумм двух костей

        Возвращает:
            словарь {сумма: количество_комбинаций}
        """
        combinations = defaultdict(int)

        for die1 in range(1, 7):
            for die2 in range(1, 7):
                combinations[die1 + die2] += 1

        return dict(combinations)

    @staticmethod
    def get_three_dice_combinations() -> Dict[int, int]:
        """
        Получение количества комбинаций для сумм трех костей

        Возвращает:
            словарь {сумма: количество_комбинаций}
        """
        combinations = defaultdict(int)

        for die1 in range(1, 7):
            for die2 in range(1, 7):
                for die3 in range(1, 7):
                    combinations[die1 + die2 + die3] += 1

        return dict(combinations)

    @staticmethod
    def get_all_dice_combinations(max_dice: int = 3) -> Dict[int, Dict[int, int]]:
        """
        Получение всех комбинаций для костей

        Аргументы:
            max_dice: максимальное количество костей

        Возвращает:
            словарь {количество_костей: {сумма: количество_комбинаций}}
        """
        result = {}

        for num_dice in range(1, max_dice + 1):
            combinations = defaultdict(int)

            for dice in itertools.product(range(1, 7), repeat=num_dice):
                combinations[sum(dice)] += 1

            result[num_dice] = dict(combinations)

        return result