In this HackerRank Iterables and iterators in python problem solution You are given a list of N lowercase English letters. For a given integer K, you can select any K indices (assume 1-based indexing) with a uniform probability from the list. Find the probability that at least one of the K indices selected will contain the letter: 'a'.

HackerRank Iterables and Iterators in python problem solution

HackerRank Iterables and Iterators in python problem solution

import sys;
import math;

def solve(n, ok, left, found):
    if (found == True):
        return 1.0;
    
    if(left == 0):
            return 0.0;
    
    ret = 0.0;
    
    ret += solve(n - 1, ok - 1, left - 1, True) * ok/n;
    ret += solve(n - 1, ok, left - 1, found) *(n - ok)/n;
    
    return ret;

n = int(sys.stdin.readline());
a = sys.stdin.readline().split();
k = int(sys.stdin.readline());

ca = len(filter(lambda x: x == 'a', a));

ret = 0.0;

#print n, a, k, ca

print solve(n, ca, k, False);