Problem 247637 · medium · Level 02 Linear Data Structures

A Good Opening Hand

probability · counting · combinations · complement rule

In a card game each player shuffles a deck and draws an opening hand of hand cards. Every set of hand cards from the deck is equally likely. The deck is described by how many cards of each kind it holds, for example {"land": 17, "spell": 23}, and a player is happy when the hand holds at least a certain number of cards of some kinds, for example {"land": 2}.

Write hand_chance(deck, hand, wants) that returns the exact probability that an opening hand meets every minimum in wants, as a tuple (numerator, denominator) in lowest terms. Kinds that are not in wants may appear any number of times.

Examples

Input:  deck = {"red": 2, "blue": 3}, hand = 2, wants = {"red": 1, "blue": 1}
Output: (3, 5)
Explanation: there are 10 possible pairs of cards; 2 × 3 = 6 of them hold one red and one blue.

Input:  deck = {"land": 17, "spell": 23}, hand = 7, wants = {"land": 2}
Output: (2152, 2405)
Explanation: of the 18643560 possible hands, 245157 have no land and 1716099 have exactly one,
so 16682304 have at least two: about 89.5%.

Constraints

  • the deck holds between 1 and 200 cards, of at most 10 kinds; 0 <= hand <= the deck size
  • wants has at most 3 kinds, all of them in deck, each with a minimum between 0 and 10
  • a probability of 0 is (0, 1)

Goals

  • Count hands of cards as unordered selections with math.comb
  • Split an event into disjoint cases and add their counts
  • Give an exact probability as a fraction in lowest terms
Starting Python…