itertools를 사용할 수 없을 때를 대비해서
def permutation(arr, r):
arr = sorted(arr)
used = [0 for _ in range(len(arr))]
result = []
def generate(chosen, used):
if len(chosen) == r:
result.append(chosen)
return
for i in range(len(arr)):
if not used[i]:
used[i] = 1
generate(chosen+[arr[i]], used)
used[i] = 0
generate([], used)
return result
print(permutation([1, 2, 3, 4 ,5], 3))
코드 참조 출처 : https://shoark7.github.io/programming/algorithm/Permutations-and-Combinations