itertools.combinations()

Sort by

recency

|

894 Discussions

|

  • + 0 comments
    from itertools import combinations
    s, n = input().split(" ")
    for i in range(1, int(n) + 1):
        [ print(''.join(x)) for  x in combinations(sorted(s),i)]
    
  • + 0 comments

    Might be lengthy but easy to understand for beginners. Hope this helps:

    from itertools import combinations
    
    S = input().split() # Splits input into a list using space from input as separator
    string = sorted(S[0])
    size = int(S[1])
    
    # Iterating combination size from 1 up to number specified in k
    i = 1
    while i <= size:
        comb = list(combinations(string, i))
        i += 1
    
        for item in comb:
            print(''.join(item).replace(" ", ""))
    
  • + 0 comments

    from itertools import combinations S,k=input().split() S="".join(sorted(S)) k=int(k) for i in range(1,k+1): li=list(combinations(S,i)) li=["".join(i) for i in li] print(*li,sep="\n")

  • + 0 comments

    Enter your code here. Read input from STDIN. Print output to STDOUT

    from itertools import combinations if name == "main": s , k = input().split() final_list=[] for i in range(1,int(k)+1): final=[] result = list(combinations(s,i)) for x in result: x = list(x) x.sort() final.append("".join(x)) final.sort() final_list.extend(final)
    print("\n".join(final_list))

  • + 0 comments

    Solution without nested for loops (better time complexity)

    from itertools import combinations, chain
    S = input().split()
    n = int(S[1])
    
    combs = chain.from_iterable(combinations(sorted(S[0]), i) for i in range(1, n + 1))
    for comb in combs:
        print(''.join(comb))