Python program to find probability of getting letter 'a' in some letters and k sized combinations

Suppose we have an array with n different English letters and a value k. We can select k different indices with uniform distribution and need to find the probability that at least one of the k indices selected will contain the letter 'a'.

For example, if we have letters = ['a', 'c', 'a', 'b', 'l', 'a', 'b', 'z'] and k = 2, we need to find all possible combinations of 2 letters and count how many contain at least one 'a'.

Algorithm

To solve this problem, we follow these steps ?

  • Initialize contain = 0 (count of combinations with 'a')
  • Initialize total = 0 (total combinations)
  • For each combination c of letters with k elements:
    • If "a" is present in c, increment contain
    • Increment total
  • Return contain/total as the probability

Example

Let us see the implementation to get better understanding ?

from itertools import combinations

def solve(letters, k):
    contain = 0
    total = 0
    
    for c in combinations(letters, k):
        if "a" in c:
            contain += 1
        total += 1
    
    return contain / total

letters = ['a', 'c', 'a', 'b', 'l', 'a', 'b', 'z']
k = 2
probability = solve(letters, k)
print(f"Probability: {probability:.4f} or {probability * 100:.2f}%")

The output of the above code is ?

Probability: 0.6429 or 64.29%

How It Works

The function uses combinations() from itertools to generate all possible k-sized combinations from the letters array. For each combination, it checks if the letter 'a' is present. The probability is calculated as the ratio of combinations containing 'a' to the total number of combinations.

Alternative Approach Using Mathematical Formula

We can also solve this using combinatorial mathematics ?

from math import comb

def solve_mathematical(letters, k):
    n = len(letters)
    count_a = letters.count('a')
    count_not_a = n - count_a
    
    # Total combinations of k elements from n
    total_combinations = comb(n, k)
    
    # Combinations with no 'a' (only from non-'a' letters)
    combinations_without_a = comb(count_not_a, k) if count_not_a >= k else 0
    
    # Combinations with at least one 'a'
    combinations_with_a = total_combinations - combinations_without_a
    
    return combinations_with_a / total_combinations

letters = ['a', 'c', 'a', 'b', 'l', 'a', 'b', 'z']
k = 2
probability = solve_mathematical(letters, k)
print(f"Mathematical approach: {probability:.4f} or {probability * 100:.2f}%")

The output of the above code is ?

Mathematical approach: 0.6429 or 64.29%

Conclusion

Both approaches give the same result. The iterative method is straightforward but less efficient for large datasets, while the mathematical approach uses combinatorial formulas for better performance. The probability represents the likelihood of selecting at least one 'a' when choosing k letters randomly.

Updated on: 2026-03-26T15:41:19+05:30

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements