Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a1d9f98e819ed70a0e994e1 Challenge 335: Five Dice 29 challenge-335

--description--

Given an array of five dice with values 1-6, return the best possible hand.

Here are the hands ranked lowest to highest:

Hand Description
"no pair" No pair or better
"pair" Two dice with the same value
"two pair" Two different pairs
"three of a kind" Three dice with the same value
"small straight" Four consecutive values
"large straight" Five consecutive values
"full house" Three of a kind and a pair
"four of a kind" Four dice with the same value
"five of a kind" All five dice with the same value

--hints--

five_dice([1, 1, 1, 1, 1]) should return "five of a kind".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([1, 1, 1, 1, 1]), "five of a kind")`)
}})

five_dice([5, 5, 5, 6, 5]) should return "four of a kind".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([5, 5, 5, 6, 5]), "four of a kind")`)
}})

five_dice([2, 5, 6, 4, 3]) should return "large straight".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([2, 5, 6, 4, 3]), "large straight")`)
}})

five_dice([4, 3, 3, 3, 1]) should return "three of a kind".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([4, 3, 3, 3, 1]), "three of a kind")`)
}})

five_dice([4, 6, 2, 6, 5]) should return "pair".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([4, 6, 2, 6, 5]), "pair")`)
}})

five_dice([1, 4, 5, 6, 2]) should return "no pair".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([1, 4, 5, 6, 2]), "no pair")`)
}})

five_dice([1, 3, 4, 6, 2]) should return "small straight".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([1, 3, 4, 6, 2]), "small straight")`)
}})

five_dice([2, 2, 5, 2, 5]) should return "full house".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([2, 2, 5, 2, 5]), "full house")`)
}})

five_dice([6, 4, 5, 6, 4]) should return "two pair".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(five_dice([6, 4, 5, 6, 4]), "two pair")`)
}})

--seed--

--seed-contents--

def five_dice(dice):

    return dice

--solutions--

def five_dice(dice):
    from collections import Counter
    counts = Counter(dice)
    values = sorted(counts.values(), reverse=True)
    keys = sorted(counts.keys())

    is_large_straight = len(keys) == 5 and keys[-1] - keys[0] == 4
    is_small_straight = not is_large_straight and any(
        keys[i + 3] - keys[i] == 3 and len(set(keys[i:i + 4])) == 4
        for i in range(len(keys) - 3)
    )

    if values[0] == 5: return "five of a kind"
    if values[0] == 4: return "four of a kind"
    if values[0] == 3 and values[1] == 2: return "full house"
    if is_large_straight: return "large straight"
    if is_small_straight: return "small straight"
    if values[0] == 3: return "three of a kind"
    if values[0] == 2 and values[1] == 2: return "two pair"
    if values[0] == 2: return "pair"
    return "no pair"