반응형
1. 순열
from itertools import permutations
for i in permutations([1,2,3,4], 2):
print(i, end=" ")
결과
(1, 2) (1, 3) (1, 4) (2, 1) (2, 3) (2, 4) (3, 1) (3, 2) (3, 4) (4, 1) (4, 2) (4, 3)
2. 조합
from itertools import combinations
for i in combinations([1,2,3,4], 2):
print(i, end=" ")
결과
(1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4)
3. 중복순열
from itertools import product
for i in product([1,2,3],'ab'):
print(i, end=" ")
결과
(1, 'a') (1, 'b') (2, 'a') (2, 'b') (3, 'a') (3, 'b')
4. 중복조합
from itertools import combinations_with_replacement
for cwr in combinations_with_replacement([1,2,3,4], 2):
print(cwr, end=" ")
결과
(1, 1) (1, 2) (1, 3) (1, 4) (2, 2) (2, 3) (2, 4) (3, 3) (3, 4) (4, 4)
반응형
'Programmers (코딩 테스트 연습)' 카테고리의 다른 글
숫자의 표현 (연습문제, Level 2) (0) | 2021.01.29 |
---|---|
짝지어 제거하기 (2017 팁스타운, Level 2) (0) | 2021.01.29 |
124 나라의 숫자 (연습문제, Level 2) (0) | 2021.01.26 |
점프와 순간 이동 (Summer/Winter Coding(~2018), Level 2) (0) | 2021.01.26 |
N진수 게임 (2018 KAKAO BLIND RECRUITMENT, Level 2) (0) | 2021.01.21 |
댓글