We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Iterables and Iterators
Iterables and Iterators
Sort by
recency
|
930 Discussions
|
Please Login in order to post a comment
this work so good and fast!!!
I know this is for itertools practice, but without itertools the answer is quite clean. Using 1 minus the multiplicative probability of not choosing 'a'.
from itertools import combinations
Input
n = int(input()) # total number of letters letters = input().split() # list of lowercase letters k = int(input()) # number of indices to choose
Generate all combinations of indices
comb = list(combinations(letters, k))
Count the combinations that contain at least one 'a'
count = 0 for c in comb: if 'a' in c: count += 1
Calculate the probability
probability = count / len(comb) print(f"{probability:.4f}")