[Python] 완전 탐색 알고리즘 - 중복 순열, 중복 조합

Saemi Min·2023년 1월 27일
0
post-thumbnail

중복 순열(Product)

개념

서로 다른 n개에서 중복이 가능하게 r개를 뽑아서 정렬하는 경우의 수

서로 다른 n개의 원소를 가지고 중복을 허용하여 r개의 원소를 선택하는 것

  • ex)

    Input: [1, 2, 3]
    Output: [ [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3] ]

코드

Git - 코드

방법 0 - 라이브러리 사용

from itertools import product

for i in product([1,2,3], repeat=2):
    print(i, end=" ")

중복 조합(Combinations_with_replacement)

개념

서로 다른 n개에서 순서 없이, 중복이 가능하게 r개를 뽑는 경우의 수

서로 다른 n개의 원소를 가지고 순서 없이, 중복을 허용하여 r개의 원소를 선택하는 것

  • ex

Input: [1, 2, 3]
Output: [ [1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3] ]


코드

Git - 코드

방법 0 - 라이브러리 사용

from itertools import combinations_with_replacement

for cwr in combinations_with_replacement([1,2,3], 2):
    print(cwr, end=" ")

출처

profile
I believe in myself.

0개의 댓글