Chance of a 15-Sum Triple from the Leftover Deck
Math & Bit ManipulationAuto-graded · 24 test casesLast updated
Compute the probability that three cards drawn without replacement from the cards the opponent left behind sum to exactly 15, using combinatorial counting.
A deck holds nine cards, one for each value from 1 to 9. Your opponent has already taken three cards from it. You will now draw three cards at random, without replacement, from the cards that remain. Every possible group of three remaining cards is equally likely to be your hand, and the order of the draw does not matter.
You win if the values in your hand add up to exactly 15.
Write a function that takes the list of cards your opponent holds and returns the probability that your hand wins.
Rules
- The remaining deck contains every value from 1 to 9 that does not appear in
opponent_cards. - A hand is any 3-element subset of the remaining cards. Each subset counts once, regardless of the order of its cards.
- The probability is the number of winning hands divided by the total number of hands.
- If no winning hand exists, return
0.0. - If fewer than 3 cards remain, there are no hands, so return
0.0. This should not raise an error. - Return a floating-point number. Do not round it.
Constraints
opponent_cardscontains exactly 3 integers, each between 1 and 9.- Under normal use the three values are distinct, so exactly 6 cards remain and there are 20 possible hands.
- If a value appears more than once in the input, just remove that value from the deck once. Hands are then formed from whatever remains.
Examples
Example 1
Input: leftover_deck_win_chance([1, 2, 3])
Output: 0.05
Example 2
Input: leftover_deck_win_chance([1, 4, 9])
Output: 0.15
Example 3
Input: leftover_deck_win_chance([2, 5, 8])
Output: 0
Example 4
Input: leftover_deck_win_chance([7, 8, 9])
Output: 0.05
Hints
Hint 1
The deck is tiny: at most 20 possible hands, so you can simply look at every one of them.
Hint 2
Because the order of the draw is irrelevant, count unordered groups of 3 (combinations), not permutations.
Hint 3
Probability = winning hands / total hands. Guard against a zero total so you never divide by zero.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
First build the remaining deck: every value from 1 to 9 that the opponent does not hold. Then enumerate every 3-card subset of that deck, count how many have a sum of 15, and divide by the total number of subsets.
Why it is correct
- Drawing 3 cards at random without replacement makes every 3-element subset equally likely. The probability is therefore (winning subsets) / (all subsets).
itertools.combinationsproduces each unordered subset of distinct cards exactly once. No card is used twice, and the same hand is never counted in two different orders.- If fewer than 3 cards remain, no subsets are produced and the total is 0. The function returns
0.0in that case instead of dividing by zero.
Useful check
Among the distinct values 1 to 9, exactly eight triples sum to 15: {1,5,9}, {1,6,8}, {2,4,9}, {2,5,8}, {2,6,7}, {3,4,8}, {3,5,7} and {4,5,6}. With three distinct opponent cards, the answer is the number of these triples that avoid every opponent card, divided by 20. For example, with [1, 4, 9] the surviving triples are {2,5,8}, {2,6,7} and {3,5,7}, giving 3/20 = 0.15.
Complexity
- Time: O(C(n, 3)) with n at most 6, so at most 20 hands. This is effectively constant.
- Space: O(n) for the list of remaining cards, since combinations are generated lazily.
Reference solution
from itertools import combinations
from typing import List
def leftover_deck_win_chance(opponent_cards: List[int]) -> float:
remaining = [card for card in range(1, 10) if card not in opponent_cards]
total = 0
winning = 0
for hand in combinations(remaining, 3):
total += 1
if sum(hand) == 15:
winning += 1
return winning / total if total else 0.0