itertools.permutations(iterable[, r])

This tool returns successive $\textbf{r}$ length permutations of elements in an iterable.  

If $\textbf{r}$ is not specified or is None, then $\textbf{r}$ 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.

Sample Code

>>> from itertools import permutations
>>> print permutations(['1','2','3'])
<itertools.permutations object at 0x02A45210>
>>> 
>>> print list(permutations(['1','2','3']))
[('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]
>>> 
>>> print list(permutations(['1','2','3'],2))
[('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
>>>
>>> print list(permutations('abc',3))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

Task  

You are given a string $\mbox{S}$. 

Your task is to print all possible permutations of size $\boldsymbol{\mbox{k}}$ of the string in lexicographic sorted order.

Input Format

A single line containing the space separated string $\mbox{S}$ and the integer value $\boldsymbol{\mbox{k}}$.

Constraints

$0<k\leq len(S)$ 

The string contains only UPPERCASE characters.

Output Format

Print the permutations of the string $\mbox{S}$ on separate lines. 

Sample Input
HACK 2

Sample Output
AC
AH
AK
CA
CH
CK
HA
HC
HK
KA
KC
KH

Explanation

All possible size $2$ permutations of the string "HACK" are printed in lexicographic sorted order.

Source: hackerrank | Difficulty: EASY


Solutions:
from itertools import permutations
(s, k) = input().split()
k = int(k)
s = ''.join(sorted(s))
for p in permutations(s, k):
	print(''.join(p))


from itertools import permutations as P
line = input().split()
s = list(line[0])
s.sort()
k = int(line[1])
a = list(P(s, k))
for i in a:
	for j in i:
		print(j, end='')
	print()


from itertools import permutations
(s, k) = input().split()
a = []
for i in permutations(s, int(k)):
	a.append(i)
a.sort()
for i in range(len(a)):
	str1 = ''.join(a[i])
	print(str1)


from itertools import permutations
(String, Value) = input().split()
for val in sorted([''.join(i) for i in list(permutations(list(String), int(Value)))]):
	print(val)


from itertools import permutations
word = input().split()
text = ''.join(sorted(word[0]))
n = int(word[1])
premu = list(permutations(text, n))
for i in premu:
	print(''.join(i))


from itertools import permutations
(string, n) = input().split()
for x in sorted(list(permutations(string, int(n)))):
	print(''.join([i for i in x]))


from itertools import permutations
(string, n) = input().split()
n = int(n)
l = sorted(list(permutations(string, n)))
for i in l:
	for j in i:
		print(j, end='')
	print()


from itertools import permutations
(S, k) = input().strip().split()
P = [''.join(i) for i in list(permutations(S, int(k)))]
P.sort()
print('\n'.join(P))


from itertools import permutations
(s, n) = input().split()
s = list(s)
s = sorted(s)
p = list(permutations(s, int(n)))
for i in p:
	print(''.join(i))


(string, k) = input().split()
from itertools import permutations
ans = []
for p in list(permutations(string, int(k))):
	ans.append(''.join(p))
ans.sort()
for q in ans:
	print(q)


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


from itertools import permutations
(S, k) = str(input()).split()
ans = list(permutations(S, int(k)))
ans.sort()
for s in ans:
	print(''.join(s[:]))


from itertools import permutations
a = input().split()
for i in sorted(permutations(a[0], int(a[1]))):
	print(''.join(i))


from itertools import permutations
(S, k) = input().split()
S = sorted(S)
k = int(k)
res = list(permutations(S, k))
for i in res:
	for ch in i:
		print(ch, end='')
	print()


from itertools import permutations
(S, k) = input().split()
k = int(k)
for i in permutations(sorted(S), k):
	print(''.join(i))


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


from itertools import permutations
(S, k) = input().split()
print('\n'.join(sorted((''.join(i) for i in permutations(S, int(k))))))


from itertools import permutations
(wss, nb) = input().split()
nb = int(nb) if nb else len(ws)
ls = [''.join(ws) for ws in permutations(wss, nb)]
ls.sort()
print(*ls, sep='\n')


from itertools import permutations
args = input().split()
S = list(args[0])
k = int(args[1])
for i in list(permutations(sorted(S), k)):
	print(''.join(i))


from itertools import permutations
inp = input().split()
for i in sorted(set(permutations(inp[0], int(inp[1])))):
	print(''.join(i))


from itertools import permutations
a = input().split(' ')
for y in permutations(sorted(a[0]), int(a[1])):
	for x in y:
		print(x, end='')
	print()


from itertools import permutations
a = list(map(str, input().split()))
word = a[0]
num = int(a[1])
perm = list(permutations(word, num))
for i in range(len(perm)):
	perm[i] = ''.join(perm[i])
perm = sorted(perm)
for i in range(len(perm)):
	print(perm[i])


from itertools import permutations
Sk = input().split()
S = Sk[0]
k = int(Sk[1])
[print(elem) for elem in sorted([''.join(perm) for perm in list(permutations(S, k))])]


from itertools import permutations
(s, k) = input().split()
k = int(k)
for i in permutations(sorted(s), k):
	print(*i, sep='')


from itertools import permutations
(s, k) = input().split()
k = int(k)
for p in sorted(permutations(s, k)):
	print(''.join(p))


from itertools import permutations
tokens = input().split()
p = permutations(sorted(list(tokens[0])), int(tokens[1]))
print('\n'.join([''.join(x) for x in p]))


from itertools import permutations
imput = input().split()
string = imput[0]
len = int(imput[1])
sol = list(permutations(string, len))
sol.sort()
for a in sol:
	for e in a:
		print(e, end='')
	print()


import itertools
(sr, nr) = input().split(' ')
s = sorted(list(sr))
r = int(nr)
for i in itertools.permutations(s, r):
	print(''.join(i))


import itertools
line = input().split()
s = line[0]
k = int(line[1])
l = []
for tup in list(itertools.permutations(s, k)):
	l.append(''.join(tup))
for t in sorted(l):
	print(t)


from itertools import permutations
line = input().split()
s = line[0]
r = int(line[1])
lst = list(permutations(s, r))
lst.sort()
if r > 1:
	for i in lst:
		print(str(i).replace('(', '').replace("'", '').replace(')', '').replace(', ', ''))
else:
	for i in lst:
		print(str(i).replace('(', '').replace("'", '').replace(')', '').replace(',', ''))


from itertools import permutations
tmp = input().split()
s = tmp[0]
n = int(tmp[1])
out = sorted(list(permutations(list(s), n)))
for p in out:
	print(''.join(p))


from itertools import permutations
(S, k) = input().split()
for p in permutations(sorted(S), int(k)):
	print(''.join(p))


from itertools import permutations
(a, b) = input().split()
for e in permutations(sorted(a), int(b)):
	print(''.join(e))


import itertools
(S, k) = input().split()
print('\n'.join((''.join(x) for x in itertools.permutations(sorted(S), int(k)))))


from itertools import permutations
elements = [i for i in input().split(' ')]
word = elements[0]
count = None
if elements[1] is not None:
	count = int(elements[1])
a = []
if count is not None:
	for el in permutations(word, count):
		a.append(''.join(el))
else:
	for el in permutations(word):
		a.append(''.join(el))
a = sorted(a)
for i in a:
	print(i)


from itertools import permutations
A = input().upper().split()
b = ''
for item in sorted(permutations(A[0], int(A[1]))):
	for i in range(len(item)):
		b += item[i]
	print(b)
	b = ''


import itertools
l = input().split()
for i in sorted(itertools.permutations(l[0], int(l[1]))):
	for j in i:
		print(j, end='')
	print()


from itertools import permutations
IN = input()
b = list(IN.split(' '))
s = sorted(b[0])
n = int(b[1])
r = list(permutations(s, n))
for i in r:
	print(''.join(i))


import itertools
(s, n) = list(map(str, input().split(' ')))
s = sorted(s)
for p in list(itertools.permutations(s, int(n))):
	print(''.join(p))


from itertools import permutations
n = input().split()
perm = list(permutations(sorted(n[0]), int(n[1])))
for i in perm:
	print(''.join(i))


from itertools import permutations
(s, k) = list(input().split())
s = sorted(s)
p = list(permutations(s, int(k)))
for item in p:
	print(''.join(item))


from itertools import permutations
k = input().split()
s = k[0]
n = int(k[1])
l = []
for i in range(0, len(s)):
	l.append(s[i])
m = list(permutations(l, n))
m.sort()
for k in range(0, len(m)):
	print(''.join(m[k]))


from itertools import permutations
(S, k) = input().split()
perms = list(permutations(sorted(list(S), key=lambda x: x[0]), int(k)))
for x in perms:
	print(''.join(x))


from itertools import permutations
(word, size) = input().split()
for i in sorted(list(permutations(word, int(size)))):
	print(''.join((j for j in i)))


from itertools import permutations
(inside, n) = input().split(' ')
results = list(permutations(inside, int(n)))
results = [''.join(result) for result in results]
results = sorted(results, key=lambda x: x)
for result in results:
	print(result)


from itertools import permutations
(s, n) = input().split()
p = list((''.join(e) for e in sorted(permutations(s, int(n)))))
for _ in p:
	print(_)


from itertools import permutations
(s, n) = input().split()
n = int(n)
vals = list(permutations(s, n))
res = []
for x in vals:
	res.append(''.join(x))
print('\n'.join(sorted(res)))


from itertools import permutations
a = input('').split(' ')
r = int(a[-1])
b = list(permutations(a[0], r))
c = []
for i in b:
	c.append(''.join(i))
c = sorted(c)
for i in c:
	print(i)


from itertools import permutations
a = list(input().split())
string = a[0]
ans = []
if len(a) > 1:
	repetition = a[1]
	ans = list(permutations(string, int(repetition)))
else:
	ans = list(permutations(string))
ans = sorted(ans)
for each_element in ans:
	combineResult = ''
	if len(a) > 1:
		for i in range(0, int(repetition)):
			combineResult = combineResult + each_element[i]
		print(combineResult)


from itertools import permutations
(a, n) = input().split()
n = int(n)
print('\n'.join(sorted([''.join(x) for x in list(permutations(a, n))])))


from itertools import permutations
(S, k) = input().split()
comb = list(permutations(sorted(S), int(k)))
for c in comb:
	print(''.join(c))


from itertools import permutations
L = list(input().split())
S = L[0]
N = int(L[1])
P = list(permutations(S, N))
P = sorted(P)
new_list = []
for words in P:
	new_list.append(''.join(words))
for i in new_list:
	print(i)


from itertools import permutations
(word, num) = input().split()
k = permutations(sorted(word), int(num))
for i in k:
	print(''.join(i))


from itertools import permutations
import sys
state = 0
for line in sys.stdin:
	(s, k) = line.strip().split(' ')
	s = sorted(s)
	k = int(k)
	p = list(permutations(s, k))
	for e in p:
		s = ''
		for i in e:
			s += str(i)
		print(s)


from itertools import permutations
(S, k) = input().split()
k = int(k)
S = sorted(S)
[print(''.join(i)) for i in list(permutations(S, k))]


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


from itertools import permutations
line = input().split()
string = ''.join(sorted(line[0]))
k = int(line[1])
outs = list(permutations(string, k))
for item in outs:
	print('%s' % ''.join(item))


from itertools import permutations
(s, n) = input().split()
n = int(n)
print('\n'.join(map(''.join, permutations(sorted(s), n))))


from itertools import product, permutations
s = input().split()
l = list(permutations(s[0], int(s[1])))
ans = []
for i in range(len(l)):
	sss = ''
	for j in range(int(s[1])):
		sss += l[i][j]
	ans.append(sss)
ans = sorted(ans)
for i in ans:
	print(i)


from itertools import permutations
line = input()
(w, l) = line.split()
for i in permutations(sorted(w), int(l)):
	print(''.join(i))


import sys
from itertools import permutations
str = input().split()
k = int(str[1])
str = str[0]
str = ''.join(sorted(str))
list1 = []
if str.isupper():
	list1 = list(permutations(str, k))
	n = len(list1)
	for i in range(0, n):
		print(''.join(list1[i]))


from itertools import permutations
(N, k) = input().split()
k = int(k)
for l in sorted(permutations(N, k)):
	print(''.join(l))


import itertools

def read_int():
	return int(input())

def read_int_words():
	return map(int, input().split())
(S, ks) = input().split()
out = sorted(itertools.permutations(S, int(ks)))
for p in out:
	print(''.join(p))


import itertools
(i, j) = input().split()
k = int(j)
b = list(map(''.join, itertools.permutations(i, k)))
b.sort()
n = len(b)
for i in range(n):
	print(b[i])


from itertools import permutations
(s, k) = input().split()
s = sorted(s)
k = int(k)
for i in permutations(s, k):
	print(''.join(i))


from itertools import permutations
(s, l) = input().split()
chars = sorted(list(s))
for x in list(permutations(chars, int(l))):
	print(''.join(x))


from itertools import permutations
I = input().split()
p = permutations(sorted(I[0]), int(I[1]))
O = [''.join(x) for x in p]
print('\n'.join(O))


from itertools import *
(s, i) = input().split()
l = list(permutations(list(s), int(i)))
for i in range(len(l)):
	l[i] = ''.join(l[i])
l.sort()
for i in l:
	print(i)


from itertools import permutations
(s, k) = input().split()
l = list(s)
l.sort()
print(*map(lambda x: ''.join(x), list(permutations(''.join(l), int(k)))), sep='\n')


import itertools
(s, k) = input().strip().split()
k = int(k)
l = list(itertools.permutations(s, k))
l.sort()
for i in range(len(l)):
	print(''.join(l[i]))


from itertools import *
c = input().split()
a = ''.join(sorted(c[0]))
b = int(c[1])
for i in list(permutations(a, b)):
	print(''.join(i))


import itertools
(N, M) = input().split()
for e in itertools.permutations(sorted(N), int(M)):
	print(''.join(e))


from itertools import permutations
s = input()
(word, n) = s.split(' ')
n = int(n)
word = ''.join(sorted(word))
perms = list(permutations(str(word), n))
for t in perms:
	print(''.join(t))


from itertools import permutations
(straw, n) = list(input().split())
n = int(n)
for s in sorted(list(permutations(straw, n))):
	print(''.join(s))


import itertools
(s, k) = input().split()
perm = itertools.permutations(sorted(list(s)), int(k))
for item in perm:
	print(''.join(item))


from itertools import permutations
(S, k) = input().split()
k = int(k)
p = permutations(sorted(list(S)), k)
[print(''.join(i)) for i in p]


from itertools import permutations
(m, n) = input().split()
k = int(n)
for i in list(permutations(sorted(m), k)):
	print(''.join(i))


from itertools import permutations
s = input().split()
for i in permutations(sorted(s[0]), int(s[1])):
	print(''.join(i))


from itertools import permutations
(s, i) = input().split()
i = int(i)
s = list(s)
s.sort()
for per in list(permutations(s, i)):
	print(''.join(per))


from itertools import permutations
(s, k) = input().split()
for e in permutations(sorted(s), int(k)):
	print(''.join(e))


from itertools import permutations
(a, b) = input().split()
a = sorted(a)
b = int(b)
c = permutations(a, b)
for i in c:
	print(''.join(i))


import itertools
Line = str(input()).split()
S = Line[0]
k = int(Line[1])
P = sorted(list(itertools.permutations(S, k)))
for T in P:
	print(''.join(T))


from itertools import permutations
(s, r) = input().split()
p = permutations(sorted(s), int(r))
for k in p:
	print(''.join(k), sep=' ')


l = input().split()
s = sorted(l[0])
k = int(l[1])
import itertools
for i in itertools.permutations(s, k):
	print(''.join(i))


import itertools
list(map(lambda i: print(''.join(i)), sorted((lambda s, i: itertools.permutations(s, int(i)))(*input().split()))))


import itertools
(a, b) = input().split(' ')
l = list(itertools.permutations(a, int(b)))
l.sort()
for t in l:
	s = ''
	for i in t:
		s = s + i
	print(s)


import itertools
input_str = input().split()
str = input_str[0]
num = int(input_str[1])
l = []
for a in itertools.permutations(str, num):
	l.append(''.join(a))
l.sort()
print('\n'.join(l))


from itertools import permutations
(txt, k) = input().split(' ')
k = int(k)
cc = list(map(''.join, permutations(txt, k)))
cc.sort()
for c in cc:
	print(c)


from itertools import permutations as p
(*a,) = input().split()
for i in p(sorted(a[0]), int(a[1])):
	print(''.join(i))


from itertools import permutations
(s, n) = input().split()
n = int(n)
d = []
P = permutations(s, n)
for i in P:
	st = ''
	for j in i:
		st += j
	d.append(st)
d.sort()
for i in d:
	print(i)


(S, a) = input().split()
from itertools import permutations
p = permutations(S, int(a))
m = sorted((''.join(a) for a in p))
for a in m:
	print(a)


from itertools import permutations
s = list(map(str, input().split()))
L = list(permutations(s[0], int(s[1])))
for i in sorted(L):
	print(''.join(i))


import itertools
(s, k) = input().split(' ')
k = int(k)
l = list(itertools.permutations(s, k))
l.sort()
for i in l:
	s = ''
	for k in i:
		s += k
	print(s)


from itertools import permutations
L = input().split()
L1 = list(permutations(L[0], int(L[1])))
L1.sort()
for i in L1:
	for k in i:
		print(k, end='')
	print()


from itertools import permutations
(n, m) = input().strip().split()
res = list(permutations(n, int(m)))
for i in sorted(res):
	print(''.join(i))


from itertools import permutations
(s, k) = input().split()
ps = permutations(sorted(s), int(k))
print('\n'.join((''.join(p) for p in ps)))


from itertools import permutations

def __starting_point():
	in_data = list(input().strip().split(' '))
	for el in permutations(sorted(in_data[0]), int(in_data[1])):
		print(''.join(el))
__starting_point()


from itertools import permutations
(s, k) = input().split()
s = sorted(s)
for i in permutations(s, int(k)):
	print(''.join(i))