You are viewing a single comment's thread. Return to all 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))
Seems like cookies are disabled on this browser, please enable them to open this website
Iterables and Iterators
You are viewing a single comment's thread. Return to all comments →
Using a custom version of combinations: