KatInterview

Chance of a 15-Sum Triple from the Leftover Deck

Math & Bit ManipulationAuto-graded · 24 test cases
EasyAuto-gradedMath & Bit Manipulationcombinatoricsprobabilityenumeration

Last 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_cards contains 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.