Iterables and Iterators

Sort by

recency

|

924 Discussions

|

  • + 0 comments
    from itertools import combinations
    N = int(input())
    letters = input().split()
    K = int(input())
    
    #Creating list of letters with r=K
    combos = list(combinations(letters, K)) 
    
    #Incrementing value of count by 1 if 'a' found in c
    count = sum(1 for c in combos if 'a' in c) 
    
    #Calculating count by total no. of c(tuples) in combos and rounding up to 3 decimal places
    print(f"{count/len(combos):.3f}") 
    
  • + 0 comments
    from itertools import combinations
    
    n= int(input())
    letters= input().split()
    k= int(input())
    
    possibilities = combinations(letters, k)
    all_= list(possibilities)
    a_Exists= []
    
    for i in all_:
        if "a" in i:
            a_Exists.append(i)
    
    print(len(a_Exists)/ len(all_))
    
  • + 0 comments

    Great for anyone looking to write cleaner, more Pythonic code. Thanks to the community for sharing tips and examples—it makes learning so much smoother! Fairplay24 pro sign up

  • + 0 comments

    Here is HackerRank Iterables and Iterators in python solution - https://programmingoneonone.com/hackerrank-iterables-and-iterators-solution-in-python.html

  • + 0 comments

    import math

    N = int(input()) all_strs = input().split() K = int(input())

    non_a_num = len(list(filter(lambda x: x!= "a", all_strs))) if non_a_num < K: print(1) else: print(1 - (math.factorial(non_a_num)/math.factorial(non_a_num-K))/(math.factorial(N)/math.factorial(N-K)))