itertools.combinations()

  • + 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(" ", ""))