본문 바로가기

코딩 테스트/알고리즘 꿀팁 정리

[파이썬 순열 조합 정리]

반응형

2. 구현(탐색)

- from collections import Counter # 딕서녀리 

- from itertools import permutations, combinations # 하나의 리스트에서 모든 케이스 구하기 

- from itertools improt product # 두 개 이상의 리스트의 조합 구하기 

3. DFS/BFS

- from collections import deque

[DFS]

- 재귀함수

[BFS]

queue = deque() (append, popleft, queue.reverse)

- 최단거리(가중치 1) 

- 인접리스트 : graph = [[] for _ in range(3)]

 

5. 이진탐색

* 순차탐색 : 리스트 안에 있는 특정한 데이터를 찾기 위해 앞에서부터 데이터를 하나씩 차례대로 확인하는 방법

* 이진탐색 : 배열 내부의 데이터가 정렬되어 있어야만 사용할 수 있는 알고리즘이다. 무작위일 때는 사용할 수 없지만 이미 정렬되어 있다면 매우 빠르게 찾아낼 수 있다. 

7. 최단경로

특정 지점까지 가장 빠르게 도달하는 방법을 찾는 알고리즘 

* 다익스트라

* 플로이드워셜 

* 포드 배만 

8. 그래프 이론 

* 노드

* 간선 

1. set 연산

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

print(a | b) # 합집합
print(a & b) # 교집합 
print(a - b) # 차집합
print(a ^ b) # 대칭차집합

2. 파이썬(Python) 리스트 모든 조합 구하기

# 2쌍의 순열 생성
from itertools import permutations
list(permutations(array, 2))


# 2쌍의 조합 생성
from itertools import combinations
list(combinations(array, 2))

# 중복 조합 
list(combinations_with_replacement([1,2,3,4],2))

# 두 개 이상 리스트의 조합 생성
from itertools import product
list(product(*items))

# 중복 순열 
list(itertools.product((["A","E","I","O","U"]), repeat=i)

 

3. 파이썬(Python) functools 모듈의 reduce 함수 

reduce(function, iterable, initializer=None)

from functools import reduc

result = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
# ((((1+2)+3)+4)+5)
print(result)

cnt = Counter([kind for name, kind in clothes])

answer = reduce(lambda x, y:x*(y+1), cnt.values(),1) -1 

반응형