Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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
0.8333
All possible unordered tuples of length 2 comprising of indices from 1 to 4 are:(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)
Out of these 6 combinations, 5 of them contain either index 1 or index 2 which are the indices that contain the letter ‘a’.
Hence, the answer is 5/6.
import math
def nCr(n,r):
f = math.factorial
return f(n) / f(r) / f(n-r)
N = input()
letters = raw_input().strip().split()
K = input()
N_a = letters.count('a')
if N_a == 0:
print '0'
elif N - N_a < K:
print '1'
else:
num = nCr(N - N_a, K)
denum = nCr(N, K)
print (denum - num) * 1.0 / denum
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import combinations
N = int(input())
L = input().split()
K = int(input())
C = list(combinations(L, K))
F = filter(lambda c: 'a' in c, C)
print("{0:.3}".format(len(list(F))/len(C)))
from __future__ import division
import itertools
n = int(raw_input())
alphas = raw_input().split()
k = int(raw_input())
checkSymbol = 'a'
combinations = list(itertools.combinations(alphas,k))
filtered = [cb for cb in combinations if checkSymbol in cb]
print len(filtered)/ len(combinations)
from itertools import combinations
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
lst = [i for i in input().split()]
k = int(input())
a_indxs = {i for i in range(1, n + 1) if lst[i - 1] == 'a'}
a_num = 0
comb_len = 0
for comb in combinations(range(1, n + 1), k):
comb_len += 1
if set(comb) & a_indxs:
a_num += 1
print(a_num / comb_len)
In our experience, we suggest you solve this The Iterables and Iterators Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
Iterables and Iterators is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution
I hope this Iterables and Iterators Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Hacker Rank Problem & Solutions >>
itertools.product() Hacker Rank Solution
String Validators Hacker Rank Solution
Text Alignment Hacker Rank solution