Iterables and Iterators

  • + 0 comments
    from itertools import combinations
    N = int(input())
    letters = input().split()
    K = int(input())
    
    #Creating list of letters with r=K
    combos = list(combinations(letters, K)) 
    
    #Incrementing value of count by 1 if 'a' found in c
    count = sum(1 for c in combos if 'a' in c) 
    
    #Calculating count by total no. of c(tuples) in combos and rounding up to 3 decimal places
    print(f"{count/len(combos):.3f}")