최종코드(코드링크)
import itertools
N, M = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
num = 0
comb = itertools.combinations_with_replacement(arr, M)
for i in comb:
for j in i:
print(j, end=' ')
print()
빅데이터분석기사 실기를 준비하며 생긴 습관 중 하나가 특정 모듈이 제공하는 함수를 출력해보는 것입니다.. ㅎㅎ
모듈의 함수를 출력하다보면 자주 쓰는 것들은 일부이고, 굉장히 많은 함수를 포함하고 있습니다.
그 중에서는 꽤 유용한 함수들이 많아 한 번 사용해보시는 것도 좋아요 :)
dir(itertools)
# 출력
# ['__doc__',
# '__loader__',
# '__name__',
# '__package__',
# '__spec__',
# '_grouper',
# '_tee',
# '_tee_dataobject',
# 'accumulate',
# 'batched',
# 'chain',
# 'combinations',
# 'combinations_with_replacement',
# 'compress',
# 'count',
# 'cycle',
# 'dropwhile',
# 'filterfalse',
# 'groupby',
# 'islice',
# 'pairwise',
# 'permutations',
# 'product',
# 'repeat',
# 'starmap',
# 'takewhile',
# 'tee',
# 'zip_longest']
위와 같이 코드를 작성하면 'itertools' 모듈이 어떤 함수를 제공하는지 모두 볼 수 있는데요,
itertools 모듈에 combination과 permutation, product 함수가 있다는건 알고 있었지만
combinations_with_replacement(중복 순열) 함수도 있었습니다 👍🏻
combinations_with_replacement을 사용하면 간단하게 통과할 수 있는 문제였습니다 :)
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준 - Python] 1208. 부분수열의 합 2 (0) | 2024.08.19 |
---|---|
[백준 - Python] 6603. 로또 (0) | 2024.08.13 |
[백준 - Python] 15656. N과 M (7) (0) | 2024.08.12 |
[백준 - Python] 15655. N과 M (6) (0) | 2024.08.12 |
[백준 - Python] 15654. N과 M (5) (0) | 2024.08.12 |