Skip to content

Commit 7e0d18e

Browse files
committed
combination
1 parent 59c2ae9 commit 7e0d18e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Concept/00_Special/Pythonic_Code_For_Coding_Test.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,33 @@ print(sorted(_list, key = lambda dt: dt.lower())) # ['CHicken', 'chocolate', 'ha
357357
Combination/Permutation
358358
-----------------------
359359

360+
가끔 모든 경우를 탐색해야 하는 상황이 있습니다. 대표적으로 이런 문제가 있겠네요?
361+
>1부터 N까지 자연수 중에서 중복 없이 M개를 고른 수열을 모두 구하시오.
360362
363+
일반적으로 백트래킹을 활용해서 문제를 풉니다. 그렇지만, 다음과 같은 기능을 공부하면 굳이 백트래킹을 할 필요가 없습니다.
364+
365+
```python
366+
import itertools
367+
_list = [1, 2, 3, 4]
368+
iter = itertools.combinations(_list, 2) # 12 13 14 23 24 34
369+
370+
iter = itertools.permutations(_list, 2) # 12 13 14 21 23 24 31 32 34 41 42 43
371+
372+
iter = itertools.combinations_with_replacement(_list, 2) # 11 12 13 14 22 23 24 33 34 44
373+
374+
iter = itertools.product(_list, repeat=2) # 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44
375+
```
376+
377+
`combination`은 모든 조합을 출력합니다. 즉, 중복이 없고, 순서를 구분하지 않습니다. `permutation`은 순열입니다. 중복은 없지만, 순서를 구분합니다.
378+
379+
`combination_with_replacement`는 중복이 가능한 조합입니다. 그래서 combination과 구분하여 11, 22, 33, 44가 새로 들어오죠. 마지막으로 `product`는 모든 가능한 경우의 수를 출력합니다. (**Cartesian Product**라고 합니다!)
380+
381+
combination을 활용하면 다음과 같은 문제도 해결할 수 있습니다.
382+
> 전체 리스트에서 3개를 추출하여 곱했을 때, 그 곱의 최댓값을 출력하는 프로그램을 작성하시오.
383+
384+
방법은 굳이 알려드리지 않아도 알겠죠?
385+
386+
이처럼 단순히 순열, 조합을 구하는 것이 아닌, 다양한 완전탐색 상황에서 사용될 수 있으니 꼭 기억해두길 바라요.
361387

362388
* * *
363389

0 commit comments

Comments
 (0)