알고리즘 문제 풀이

[백준 - Python] 15657. N과 M (8)

김혠 2024. 8. 12. 11:42

문제링크

 

최종코드(코드링크)

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을 사용하면 간단하게 통과할 수 있는 문제였습니다 :)