Iterables and Iterators

Sort by

recency

|

910 Discussions

|

  • + 0 comments

    just for reference

    from itertools import combinations num=int(input()) #taking n inputs letter=input().split()# taking letter input k=int(input()) #taking combinations input eg 2=ag combination=list(combinations(letter,k)) #using list() to get length combi_a=0 for i in combination: if "a" in i: combi_a+=1 #add one if a is found in i(combination) print(combi_a/len(combination))

  • + 0 comments
    from math import factorial as fac
    
    n=int(input())
    list=input().split()
    m=int(input())
    
    # total events: nCm combination formula
    total=fac(n)/(fac(m)*fac(n-m))
    
    # events when selected indices doesn't contains "a"
    n_dup=len(list)-list.count('a')
    
    # if selecting indices is not possible without picking'a'
    if n_dup<m:
        result=1
    else:
        prob_bar=fac(n_dup)/(fac(m)*fac(n_dup-m))
        result=(total-prob_bar)/total
        
    print(result)
    
  • + 0 comments

    from itertools import combinations n,lst,k = int(input()),input().split(),int(input()) comb_lst = list(combinations(list(range(n)),k)) a_index = [] for i in range(len(lst)): if lst[i] == "a": a_index.append(i) counter = 0 for sublist in comb_lst: for i in sublist: if i in a_index: counter += 1 break probab = counter/len(comb_lst)
    print(probab)

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

      You create the combinations 2 times instead of just one time which would be double the work instead of saving the combinations in a variable. just fyi