언어
ct 정리
땅호720
2024. 2. 21. 21:26
hash() 하나만 찾아야 할 때 유용 ex: 마라톤 미완주자
from collections import Counter
Counter: 딕셔너리 형태로 key가 몇 개 (value) 있는지 자동처리
sorted(옵션): reverse=True, key=(lambda x: x[1])
str startswith zfill upper lower isupper islower
ascii 알파벳 26개
map(func, iter)
dict[key] = value value가 list가 들어가면 +로 append 가능
for key in dict
for k,v in dict.items() -> tuple
[x for x in list if x > 2]
[[False] * n for _ in range(m)]
ex: 가장 큰 수 numbers.sort(key=lambda x:str(x)*3, reverse=True)
-> 원소가 1000 이하 => 최소 3자릿 수
ex: H-index
-> enumerate(citations, start=1) => 논문 idx(수)와 인용 수
max(map(min, enumerate(citations, start=1)))
import heapq -> 오름차순으로 자동 정렬해주는 queue (우선순위 큐)
heapq.heapify(list) -> list를 queue로 변환
heapq.heappop(list) -> 왼쪽, 최솟값 pop
heapq.heappush(list, value) -> push & 정렬
-> list가 value로 들어가면 앞선 값 순으로 정렬 ex: 디스크 컨트롤러
q,r = divmod(dividend, divisor)
// 몫 % 나머지 ** 거듭제곱 (**0.5 루트, math.sqrt)
& | ^ ~ >> << 비트연산자
^ : 하나만 true일 때 true, 본인과 ^는 0 출력
BFS: 인접노드, 최단거리
deque: append, popleft, pop, 인덱스 접근 가능
from collections import deque
while queue:
for word in words:
count += 1
if count==1:
queue.append([word, step+1])
dx = [-1,1,0,0]
dy = [0,0,-1,1]
while queue:
for i in range(4): #EWSN
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<m and 0<=ny<n and maps[ny][nx]==1:
visited[ny][nx]=True
queue.append((ny, nx))
maps[ny][nx] = maps[y][x]+1
DFS: 미로찾기, 깊게, 모든 경우의 수, 덩어리 찾기(노드)
global 이용 * 재귀함수
- dfs 끝내면 count+=1 (덩어리 완료)
dfs(v,n,computers)
- dfs 내에서 answer+=1,
dfs(numbers, target:int, idx=0, value=0)
-> 시작은 dfs(numbers, target)
이후, dfs(numbers, target, idx+1, value+numbers[idx])