Iterables and Iterators

Sort by

recency

|

930 Discussions

|

  • + 0 comments

    this work so good and fast!!!

    import itertools
    N =int(input())
    cadena =input().split()
    k=int(input())
    
    print(len(list(itertools.filterfalse(lambda a: 'a' not in a,itertools.combinations(cadena, k))))/(len (list(itertools.combinations(cadena, k)))))
    
  • + 0 comments
    from itertools import combinations
    
    if __name__ == '__main__':
        length,l1,K = int(input()),input().split(),int(input())
        list_combos = list(combinations(l1, K))
        counter1 = 0
        for comb in list_combos:
                if 'a' in comb:
                    counter1 += 1
        print(counter1 / len(list_combos))
    
  • + 0 comments

    I know this is for itertools practice, but without itertools the answer is quite clean. Using 1 minus the multiplicative probability of not choosing 'a'.

    N,s,n = int(input()),input(),int(input())
    x = 1
    b = N-s.count('a')
    for i in range(n):
        x = x*((b-i)/(N-i)) 
    print(1-x)
    
  • + 0 comments

    from itertools import combinations

    Input

    n = int(input()) # total number of letters letters = input().split() # list of lowercase letters k = int(input()) # number of indices to choose

    Generate all combinations of indices

    comb = list(combinations(letters, k))

    Count the combinations that contain at least one 'a'

    count = 0 for c in comb: if 'a' in c: count += 1

    Calculate the probability

    probability = count / len(comb) print(f"{probability:.4f}")

  • + 0 comments
    from itertools import combinations
    
    N = int(input())
    
    englishLetters = input().split()
    
    let = 'a'
    
    K = int(input())
    
    l1 = list(combinations(englishLetters,K))
    
    # print(l1)
    favOutcome = 0
    totOutcome = len(l1)
    
    for item in l1:
        if 'a' in item:
            favOutcome += 1    
    
        
                
    
    prob = favOutcome/totOutcome
    
    
    print(prob)