HackerRank-Python

Prepare> Python > Itertools > itertools.permutations()

stem_sw 2023. 8. 5. 22:11
 

itertools.permutations() | HackerRank

Find all permutations of a given size in a given string.

www.hackerrank.com

 

문제


Task

You are given a string S.
Your task is to print all possible permutations of size k of the string in lexicographic sorted order.

 

=> S로 길이가 k인 순열들을 정렬해 출력하라

 

 

 

 

코드


from itertools import permutations

S, k = input().split()

A = sorted(list(permutations(S, int(k))))

for i in range(len(A)):
    print(
        ''.join(A[i])
    )

 

 

 

 

노트


print(
	'\n'.join(sorted(''.join(p) for p in permutations(S,k)))
)
  • 압축 형식인가

 

 

두 번째 시도

from itertools import permutations

S, k = input().split()

li_s= sorted(list(S))

for i in list(permutations(li_s, int(k))):
    print(''.join(i))

 

 

 

 

참조


 

HackerRank itertools.permutations() solution in Python

HackerRank itertools.permutations() solution in Python2, python3 and pypy, pypy3 programming language with practical program code example explaination

programs.programmingoneonone.com