Iterables and Iterators

  • + 0 comments

    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):

    if any(letters[i] == 'a' for i in comb):
        favorable_combinations += 1
    

    probability = favorable_combinations/total_combinations print(f"{probability:.4f}")