count-combinations
When to Use
- Probability calculations
- Counting permutations or combinations
- Enumerating all possibilities (brute force)
- Monte Carlo simulation
- Card game probabilities
- Dice roll distributions
- Urn/ball problems
When NOT to Use
- When closed-form formula exists and is simpler
- Astronomically large sample spaces (use simulation)
- When approximation is acceptable (use sampling)
The Pattern
Define sample space explicitly, then count favorable outcomes.
from fractions import Fraction
from itertools import combinations, permutations, product
def P(event, space):
"""Probability = favorable outcomes / total outcomes."""
favorable = event & space if isinstance(event, set) else {x for x in space if event(x)}
return Fraction(len(favorable), len(space))
# Sample spaces
die = {1, 2, 3, 4, 5, 6}
two_dice = {(a, b) for a in die for b in die}
deck = [r + s for r in 'A23456789TJQK' for s in 'SHDC']
hands = set(combinations(deck, 5))
# Events as sets or predicates
even = {2, 4, 6}
is_flush = lambda hand: len(set(c[1] for c in hand)) == 1
Example (from pytudes Probability.ipynb)
from fractions import Fraction
from itertools import combinations
def P(event, space):
"""The probability of an event, given a sample space."""
favorable = {x for x in space if x in event} if isinstance(event, set) \
else {x for x in space if event(x)}
return Fraction(len(favorable), len(space))
# Urn problem: 6 blue, 9 red, 8 white balls; draw 6
def balls(color, n):
return [f'{color}{i}' for i in range(1, n + 1)]
urn = balls('B', 6) + balls('R', 9) + balls('W', 8)
U6 = set(combinations(urn, 6))
def select(color, n, space=U6):
"""Event: exactly n balls of given color."""
return {s for s in space if sum(1 for b in s if b[0] == color) == n}
# Probability of drawing 3 blue, 1 red, 2 white
P(select('B', 3) & select('R', 1) & select('W', 2), U6)
# Returns: Fraction(240, 4807)
Key Principles
- Enumerate explicitly: When feasible, list all outcomes
- Use Fraction: Exact arithmetic, no floating point errors
- Events as sets: Use set operations (union, intersection)
- Events as predicates: Use functions for complex conditions
- itertools for generation:
combinations, permutations, product
1---2name: count-combinations3description: For probability and counting: permutations, combinations, sample spaces, Monte Carlo simulation, brute-force enumeration, card/dice problems.4---56# count-combinations78## When to Use9- Probability calculations10- Counting permutations or combinations11- Enumerating all possibilities (brute force)12- Monte Carlo simulation13- Card game probabilities14- Dice roll distributions15- Urn/ball problems1617## When NOT to Use18- When closed-form formula exists and is simpler19- Astronomically large sample spaces (use simulation)20- When approximation is acceptable (use sampling)2122## The Pattern2324Define sample space explicitly, then count favorable outcomes.2526```python27from fractions import Fraction28from itertools import combinations, permutations, product2930def P(event, space):31 """Probability = favorable outcomes / total outcomes."""32 favorable = event & space if isinstance(event, set) else {x for x in space if event(x)}33 return Fraction(len(favorable), len(space))3435# Sample spaces36die = {1, 2, 3, 4, 5, 6}37two_dice = {(a, b) for a in die for b in die}38deck = [r + s for r in 'A23456789TJQK' for s in 'SHDC']39hands = set(combinations(deck, 5))4041# Events as sets or predicates42even = {2, 4, 6}43is_flush = lambda hand: len(set(c[1] for c in hand)) == 144```4546## Example (from pytudes Probability.ipynb)4748```python49from fractions import Fraction50from itertools import combinations5152def P(event, space):53 """The probability of an event, given a sample space."""54 favorable = {x for x in space if x in event} if isinstance(event, set) \55 else {x for x in space if event(x)}56 return Fraction(len(favorable), len(space))5758# Urn problem: 6 blue, 9 red, 8 white balls; draw 659def balls(color, n):60 return [f'{color}{i}' for i in range(1, n + 1)]6162urn = balls('B', 6) + balls('R', 9) + balls('W', 8)63U6 = set(combinations(urn, 6))6465def select(color, n, space=U6):66 """Event: exactly n balls of given color."""67 return {s for s in space if sum(1 for b in s if b[0] == color) == n}6869# Probability of drawing 3 blue, 1 red, 2 white70P(select('B', 3) & select('R', 1) & select('W', 2), U6)71# Returns: Fraction(240, 4807)72```7374## Key Principles751. **Enumerate explicitly**: When feasible, list all outcomes762. **Use Fraction**: Exact arithmetic, no floating point errors773. **Events as sets**: Use set operations (union, intersection)784. **Events as predicates**: Use functions for complex conditions795. **itertools for generation**: `combinations`, `permutations`, `product`