itertools

itertools.permutations()

itertools.permutations(iterable[, r])

This tool returns successive length permutations of elements in an iterable. If is not specified or is None, then defaults to the length of the iterable, and all possible full length permutations are generated. Permutations are printed in a lexicographic sorted order. So, if the input iterable is sorted, the permutation tuples will be produced in a sorted order.

My solution

from itertools import permutations
inp = input().split()
s, k, result = inp[0], int(inp[1]), []
[result.append(''.join(list(i))) for i in list(permutations(s, k))]
result.sort()
[print(i) for i in result]

Adapted solution

from itertools import permutations
s, k = input().split()
print(*[''.join(i) for i in permutations(sorted(s), int(k))], sep='\n')

itertools.combinations()

itertools.combinations(iterable, r)
This tool returns the length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

My solution

from itertools import combinations
s, k = input().split()
print(*[''.join(j) for i in range(int(k)+1) for j in combinations(sorted(s), i)
if ''.join(j) != ''], sep='\n')

Adapted solution

no adaptions


You'll only receive email when understanding publishes a new post

More from understanding