Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Iterables and Iterators solution in python

YASH PAL, 31 July 2024
In this Hackerrank Iterables and Iterators problem we have
Input Format
The input consists of three lines. The first line contains the integer, denoting the length of the list. The next line consists of space-separated lowercase English letters, denoting the elements of the list.
The third and the last line of input contains the integer, denoting the number of indices to be selected.
Output Format
Output a single line consisting of the probability that at least one of the indices selected contains the letter:”.
Note: The answer must be correct up to 3 decimal places.
HackerRan Iterables and Iterators solution in python

Topics we are covering

Toggle
  • Problem solution in Python 2 programming.
  • Problem solution in Python 3 programming.
    • Problem solution in pypy programming.
    • Problem solution in pypy3 programming.

Problem solution in Python 2 programming.

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

Problem solution in Python 3 programming.

# 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)))

Problem solution in pypy programming.

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)

Problem solution in pypy3 programming.

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)
        

coding problems solutions Python Solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes