코딩테스트 때 쓰이는 것들 모음
import heapq # 기본은 min-heap, max-heap 사용시 -값 넣기
lst = [1, 2, 3]
heapq.heapify(lst)
# 빈 배열은 push시 자동으로 힙으로 됨
empty = []
heapq.heappop(my_list)
heapq.heappush(my_list, -1)
my_list[0]
from collections import deque
que = deque([1, 2, 3])
que.insert(i, num)
que.append(item)
que.popleft()
que[0]
import collections
my_list = ["a", "a", "b", "b", "c"]
collections.Counter(my_list)
# Counter({'a': 2, 'b': 2, 'c': 1})
import itertools
from itertools import permutations
from itertools import combinations
from itertools import product
from itertools import chain
permutations(range(1, 10), 3)
permutations(lst)
combinations('ABC', 2)
product(range(1,n1+1), range(1,n2+1), range(1,n3+1))
or_not = [(0, 500), (0, 1500)]
list(product(*or_not))
#[(0,0), (0,1500), (500,0), (500,1500)]
itertools.chain(*board)
chain(*board)
my_dict['total']
my_dict.get('total', 0)
my_dict.pop('홍길동', 180)
dict.items()
dict.keys()
dict.values()
nodes = {}
for v1, v2 in edge:
nodes[v1] = nodes.get(v1, []) + [v2]
nodes[v2] = nodes.get(v2, []) + [v1]
# {0: [1, 2], 1: [0, 3, 4], 2: [0], 3: [1], 4: [1]}
nodes = {}
for v1, v2, d in road:
nodes[v1] = nodes.get(v1, []) + [(v2, d)]
nodes[v2] = nodes.get(v2, []) + [(v1, d)]
dist = { i:float('inf') if i != 1 else 0 for i in range(1, N+1) }
from collections import defaultdict
dict1 = defaultdict(set)
dict2 = defaultdict(list)
dict3 = defaultdict(int) -> 0으로 셋팅
from collections import defaultdict
nodes = defaultdict(list)
for v1, v2 in edge:
nodes[v1].append(v2)
nodes[v2].append(v1)
my_set.add(3)
my_set.remove(3)
if 3 in my_set:
print('3 있음')
if 3 not in my_set:
print('3 없음')
set1 | set2
set1.update(set2)
set1 - set2
set1 & set2
from bisect import bisect_left, bisect_right
bisect_left(lst, start)
bisect_right(lst, end)
def count_by_lange(lst, start, end):
return bisect_right(lst, end) - bisect_left(lst, start)
DELTAS = [(0, 1), (0, -1), (-1, 0), (1, 0)] # 오른쪽 왼쪽 위 아래
DELTAS = [(-1, 0, UP), (1, 0, DOWN), (0, 1, RIGHT), (0, -1, LEFT)]
DELTAS = {'U':(0, 1), 'D':(0, -1), 'L':(-1, 0), 'R':(1, 0)}
DELTAS = {'U': (-1, -1), 'D': (1, 0), 'R': (0, 1)}
NEXT = {'U': 'D', 'D': 'R', 'R': 'U'}
def visitable(y, x, m, n):
return 0 <= y < m and 0 <= x < n
N, primes = end_num, set()
prime_check = [False, False] + [True]*(N-1)
for i in range(2, N+1):
if prime_check[i]:
primes.add(i)
prime_check[i*2::i] = [False] * len(prime_check[i*2::i])
from fractions import gcd
gcd(6,8) # 2
def lcm(a,b):
return a * b // gcd(a, b)
[(number//i, i) for i in range(1, int(number**.5)+1) if number % i == 0]
# number = 24 -> 곱해서 24가되는 경우
[(24, 1), (12, 2), (8, 3), (6, 4)]
def impossible(n, middle, times):
return sum([middle // x for x in times]) < n
def solution(n, times):
left, right = 1, max(times)*n
while left < right:
middle = (left + right) // 2
if impossible(n, middle, times):
left = middle + 1
else:
right = middle
return left
def is_poss(middle, budgets, M):
return M >= sum([min(middle, budget) for budget in budgets])
def solution(budgets, M):
left, right = 1, max(budgets)
while left < right:
middle = (left + right + 1) // 2
if is_poss(middle, budgets, M):
left = middle
else:
right = middle - 1
return right
lst.sort()
dist.sort(reverse=True)
lst.sort(key= lambda x : (x*4)[:4], reverse=True)
sorted(lst)
sorted(lst, reverse=True)
sorted(answer, key = lambda x : (x[0], -x[1], x[2]))
lst.count(0)
[*row, *col, *diag].count(n)
filter = [item for item in items if 조건식 ]
len(filter)
board = [[0] * n for i in range(m)]
reversed = list(map(list,zip(*board)))
def rotate90(arr):
return list(zip(*arr[::-1]))
query.replace('?','a')
s.split("},{")
number = int(''.join(x))
splited = [s[i:i+size] for i in range(0, LENGTH, size)]
# size = 3 : aabbaccc -> ['aab', 'bac', 'cc']
import re
re.search("^"+pre+".+",number)
re.findall(query, word)
lst = re.findall('\d+',s)
# ['2', '2', '1', '3', '4']
re.compile("(\D)")
weak_point = weak + [w+n for w in weak]
import math
math.ceil(num / div)
math.factorial(3)