Iterables and Iterators

Sort by

recency

|

893 Discussions

|

  • + 0 comments

    from itertools import combinations n=int(input()) a=list(input().split()) k=int(input()) b=list(combinations(a,k)) count=sum(1 for x in b if 'a' in x) print(count/len(b))

  • + 0 comments
    from itertools import combinations
    n = int(input().strip())
    A = tuple( input().strip().split(" "))
    K = int(input().strip())
    
    B = list(combinations(A,K))
    print(len(list(filter( lambda x : "a" in x , B )))/len(B))
    
  • + 0 comments

    from itertools import combinations n=int(input()) y=input().split() f=int(input()) c=list(combinations(y,f)) d=0 for i in c: if "a" in i: d+=1 print(d/len(c))

  • + 0 comments

    For coders who want to challenge yourself don't use itertools lib Using The Recursion :D

    count = 0
    x = [list(map(str, input().split())) for _ in range(3)]
    data = " ".join(x[1])
    data = sorted(data.replace(" ",''))
    
    def my_combinstion(a: str, r, result=None, indices=None, index=0):
        
        if result is None:
            result = []
        if indices is None:
            indices = []
            
        if index == r:
            pair = tuple(a[i] for i in indices)
            result.append(pair)
            return 
            
        start = indices[-1] + 1 if indices else 0
        for i in range(start, len(a)):
            my_combinstion(a, r, result, indices +[i], index+1)
            
        return result
    
        
    z = my_combinstion(data, int(x[-1][-1]))
    for x in z:
        if 'a' in x:
            count+=1
    print(count/len(z))
    
  • + 0 comments
    from itertools import combinations
    def probability(letter, K):
        final = list(combinations(letter,K))
        count = 0 
        for i in range(len(final)):
            if 'a' in final[i]:
                count += 1 
        prob = count/len(final)
        return prob
        
    if __name__ == "__main__":
        N = int(input())
        letter = list(input().split())
        K = int(input())
        result = probability(letter,K)
        print(result)