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
|
910 Discussions
|
Please Login in order to post a comment
just for reference
from itertools import combinations num=int(input()) #taking n inputs letter=input().split()# taking letter input k=int(input()) #taking combinations input eg 2=ag combination=list(combinations(letter,k)) #using list() to get length combi_a=0 for i in combination: if "a" in i: combi_a+=1 #add one if a is found in i(combination) print(combi_a/len(combination))
from itertools import combinations n,lst,k = int(input()),input().split(),int(input()) comb_lst = list(combinations(list(range(n)),k)) a_index = [] for i in range(len(lst)): if lst[i] == "a": a_index.append(i) counter = 0 for sublist in comb_lst: for i in sublist: if i in a_index: counter += 1 break probab = counter/len(comb_lst)
print(probab)
You create the combinations 2 times instead of just one time which would be double the work instead of saving the combinations in a variable. just fyi