Iterables and Iterators Hackerrank solution python
The itertools module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an iterator algebra making it possible to construct specialized tools succinctly and efficiently in pure Python.
To read more about the functions in this module, check out their documentation here.
You are given a list of N lowercase English letters. For a given integer K, you can select any K indices (assume 1-based indexing) with a uniform probability from the list.
Find the probability that at least one of the K indices selected will contain the letter: ‘a’.
The input consists of three lines. The first line contains the integer N, denoting the length of the list. The next line consists of N space-separated lowercase English letters, denoting the elements of the list.
The third and the last line of input contains the integer K, denoting the number of indices to be selected.
Output a single line consisting of the probability that at least one of the K indices selected contains the letter:’a’.
Note: The answer must be correct up to 3 decimal places.
All the letters in the list are lowercase English letters.
4 a a c d 2
All possible unordered tuples of length 2 comprising of indices from 1 to 4 are:
Out of these 6 combinations,5 of them contain either index 1 or index 2 which are the indices that contain the letter ‘a’.
from itertools import combinations N=int(input()) L=list(map(str,input().split())) K=int(input()) count = 0; total = 0; for i in combinations(L,K): count += 'a' in i total += 1 print(count/total)
Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.