- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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. We also have another value k. We can select k different indices (1-indexed) with uniform distribution. We have to find the probability that at least one of the k indices selected will contain the letter 'a'.

So, if the input is like letters = ['a', 'c', 'a', 'b', 'l', 'a', 'b', 'z'] k = 2, then the output will be 64.28%. There are combinations like (1, 2), (1, 3) like there are 28 combinations but some of them like (1,2), (1,3), (6,7) such 18 pairs are holding 7, so 18/28 = 0.6428.

To solve this, we will follow these steps −

- contain := 0
- total := 0
- for each combination c of letters with k elements, do
- if "a" is present in c, then
- contain := contain + 1

- total := total + 1

- if "a" is present in c, then
- return contain/total

## Example

Let us see the following 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 print(solve(letters, k))

## Input

['a', 'c', 'a', 'b', 'l', 'a', 'b', 'z'], 2

## Output

0

- Related Articles
- Python program to find probability of getting letter 'a' in some letters and k sized combinations
- Python program to find N-sized substrings with K distinct characters
- Program to find list of all possible combinations of letters of a given string s in Python
- Letter Combinations of a Phone Number in Python
- Program to find number of distinct combinations that sum up to k in python
- Program to find n length string made of letters from m sized alphabet with no palindrome in Python
- Program to find a list of numbers where each K-sized window has unique elements in Python
- C++ program to find maximum of each k sized contiguous subarray
- Program to find k-sized list where difference between largest and smallest item is minimum in Python
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Program to find number of subsequences with i, j and k number of x, y, z letters in Python
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python – N sized substrings with K distinct characters
- Program to find number of combinations of coins to reach target in Python
- Program to find probability of getting assigned seat for the last person in an airplane after seat shuffling in Python
- Probability of getting at least K heads in N tosses of Coins in C++

Advertisements