Iterables and Iterators

Sort by

recency

|

937 Discussions

|

  • + 0 comments

    Using a custom version of combinations:

    def mycombinations(S, K):
        arr = [0] * K
        cpt = 0
        for i in range(K):
            arr[i] = i 
     
        idx = K - 1     # index in arr (from 0 to K)
        allSets = []
        while True:
            myset = []
            for i in range(K - 1, -1, -1):
                myset.append(S[arr[idx - i]])
            allSets.append(myset)
            
            
            while True:       
                arr[idx] += 1
                while arr[idx] > len(S) - K + idx:
                    idx -= 1
                    if (idx < 0):                    
                        return allSets
                        
                    arr[idx] += 1
                    
                while idx < K - 1:
                    arr[idx + 1] = arr[idx] + 1   
                    idx += 1
                    
                else:
                    break       
                    
        return allSets
    
    N = input()
    S = input().split()
    K = int(input())
    ALLSETS = list(mycombinations(S, K))
    COUNT = 0
    
    for c in ALLSETS:
        if 'a' in c:
            COUNT += 1
    
    print(round(COUNT/len(ALLSETS), 11))
    
  • + 0 comments

    from itertools import combinations

    A = input() B = list(input().split(" ")) C = input()

    D = list(combinations(B, int(C)))

    count=0 for i in D: if "a" in i: count = count+1

    print(count/len(D))

  • + 0 comments
    from itertools import combinations
    
    N = input()
    S = input().split()
    K = int(input())
    ALL = list(combinations(S, K))
    COUNT = sum(1 for UNITS in ALL if 'a' in UNITS)
    print(round(COUNT/len(ALL), 4))
    
  • + 0 comments
    from itertools import combinations
    n = int(input())
    s = list(input().split(" "))
    k = int(input())
    count = 0
    for tup in combinations(s,k):
        if any(elem == 'a' for elem in tup):
            count += 1
    print(count / len(list(combinations(s,k))))
    
  • + 0 comments
    from itertools import combinations
    
    length = int(input())
    s = input().split()
    k = int(input())
    count = 0
    for tup in combinations(s,k):
        if any(elem == 'a' for elem in tup):
            count += 1
    
    print(count / len(list(combinations(s,k))))