Calculating combinations in Python. The following code shows how to calculate combinations in the python programming language. The following shows how to solve the HackerRank itertools.combinations() Python itertools problem.
Code:
import itertools
S, k = input().split()
S = sorted(S)
comb_list = []
for x in range(1, int(k)+1):
combinations = list(itertools.combinations(S, x))
comb_list += [ ''.join(combination) for combination in combinations ]
for comb in comb_list:
print(comb)
Test Input:
PQRS 2
Test Output:
P
Q
R
S
PQ
PR
PS
QR
QS
RS