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
|
887 Discussions
|
Please Login in order to post a comment
Enter your code here. Read input from STDIN. Print output to STDOUT
import itertools N = int(input()) letters = input().split() k = int(input()) listed_combinations = list(itertools.combinations(letters,k))
count = 0
for i in listed_combinations: if "a" in i: count +=1
print(count/len(listed_combinations))
from itertools import combinations
Reading input
n = int(input())
letters = input().split()
k = int(input())
Finding the indices where the letter 'a' occurs
a_indices = [i for i, letter in enumerate(letters) if letter == 'a']
Generating all combinations of k indices from the list
total_combinations = list(combinations(range(n), k))
Counting the combinations that contain at least one 'a' index
favorable_combinations = [ combo for combo in total_combinations if any(i in a_indices for i in combo) ]
Calculating the probability
probability = len(favorable_combinations) / len(total_combinations)
Printing the result with 4 decimal places print(f"{probability:.4f}")
import itertools
n = int(input())
letters = input().split()
k = int(input())
count = letters.count('a')
total_combinations = len(list(itertools.combinations(range(n),k)))
favorable_combinations = 0
for comb in itertools.combinations(range(n), k):
probability = favorable_combinations/total_combinations print(f"{probability:.4f}")
Please, can anybody answer me, why itertools.combinations() is fine, but interpreter cannot recognize itertools.ifilter()? I see that using sum with a list comprihension is easier, I used built in filter() with success, though.