Drill

ProblemsPython › games

Score dice pairs

hardgamesPython

A dice game awards points from matching pairs. For each face value that appears more than once, every pair of matching dice contributes the face value to the total score.

dice_pairs_score(dice: list<int>) → int

Solve it in the editor →

Where you start

def dice_pairs_score(dice: list[int]) -> int:
    

Worked examples

CallResult
dice_pairs_score([2, 3, 2, 5, 3])5
dice_pairs_score([6, 6, 6])18
dice_pairs_score([1, 2, 3])0
dice_pairs_score([4, 4, 4, 4])24

Hint

Count frequencies, then for each face with two or more occurrences add value * count * (count - 1) / 2.

Reference solution in Python
def dice_pairs_score(dice: list[int]) -> int:
    freq = {}
    for d in dice:
        freq[d] = freq.get(d, 0) + 1
    total = 0
    for v, k in freq.items():
        if k >= 2:
            total += v * k * (k - 1) // 2
    return total

The same problem in another language

More games problems in Python