Calculating permutations in Python

Calculating permutations in Python

Instructor-svgAl-Mamun Sarkar
Mar 26 , 2020

Calculating permutations in Python. The following code shows how to calculate permutations in the python programming language. The following shows how to solve the HackerRank itertools.permutations() Python itertools problem.

Code:

import itertools

S, k = input().split() 
permutations = list(itertools.permutations(sorted(S), int(k)))
perm_list = [ ''.join(permutation) for permutation in permutations ]

# perm_list.sort()
for perm in perm_list:
	print(perm)

 

Test Input:

ABCD 2

Test Output:

AB
AC
AD
BA
BC
BD
CA
CB
CD
DA
DB
DC
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram