Iterables and Iterators

Sort by

recency

|

907 Discussions

|

  • + 0 comments
    from itertools import combinations
    
    n, lc_letters, k = int(input()), input().split(), int(input())  # length of list
    
    combi = list(combinations(lc_letters, k))
    
    num_a_in_comb = 0
    for comb in combi:
        if "a" in comb:
            num_a_in_comb += 1
            
    print (num_a_in_comb/len(combi))
    
  • + 1 comment
    import itertools
    
    N, cases, K = int(input()), input().split(), int(input())
    prob_consist_a = len([combo for combo in itertools.combinations(cases, K) if 'a' in combo]) / len(list(itertools.combinations(cases, K)))
    print(prob_consist_a)
    
  • + 0 comments

    import itertools List=[] count = 0 n = int(input()) l = input().split(" ") k = int(input()) for i in range(len(l)): if(l[i]=="a"): List.append(i+1) comb = list(itertools.combinations([i+1 for i in range(len(l))],k)) for i in comb: for j in List: s="" if(j in i): s+=" " if(len(s)>=1): count+=1 break print(count/len(comb))

  • + 0 comments
    import itertools
    
    N = int(input())
    cases = list(input().split())
    K = int(input())
    
    all_combinations = list(itertools.combinations(cases, K))
    
    valid_combinations = [combo for combo in all_combinations if 'a' in combo]
    
    prob_consist_a = len(valid_combinations) / len(all_combinations)
    print(prob_consist_a)
    
  • + 0 comments

    n = int(input()) # Length of the list letters = input().split() # List of letters k = int(input()) # Number of indices to select

    comb = list(combinations(letters, k)) favorable = sum(1 for group in comb if 'a' in group) probability = favorable / len(comb) print(f"{probability:.3f}")