Iterables and Iterators

  • + 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))