์๊ณ ๋ฆฌ์ฆ์ ํ์ฉ๋๋ ํ์ด์ฌ ๋ฌธ๋ฒ
heap์๋ฃํ
1
2
3
4
5
6
|
import heapq
heap=[]
heapq.heappush(heap,4)
heapq.heappush(heap,6)
result=heapq.heappop(heap)
|
cs |
์ด๋, ๋ฆฌ์คํธ๋ฅผ ํ์ ์์๋ก ๋ฃ์ผ๋ฉด ๋ฆฌ์คํธ์ ์ฒซ ๋ฒ์งธ ์์์ ๋ํด ์ฐ์ ์์๋ฅผ ํ๋จํ์ฌ ํ ์๋ฃ๊ตฌ์กฐ์ ๋ฃ๋๋ค.
deque์๋ฃํ
1
2
3
4
5
|
from collections import deque
deq=deque([])
deq.appendleft(10)
deq.popleft()
|
cs |
๋น์ฐํ pop์ด๋ append๋ ์ง์ํ๋ค. ๋ฆฌ์คํธ์ ๋ฐ์ ํํ๋ผ๊ณ ์๊ฐํ๋ฉด ๋จ.
defaultdict ์๋ฃํ
ํ์ด์ฌ์ ๊ธฐ๋ณธ dictionary์ ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋ฅ์ ๋์ผํ๋ค.
๋จ, ํ์ด์ฌ dict๋ ์กด์ฌํ์ง ์๋ key๋ฅผ index๋ก ์ ๋ฌ๋ฐ์์ ๋ ์๋ฌ๋ฅผ ๋ฐ์์ํค์ง๋ง
defaultdict๋ ์กด์ฌํ์ง ์๋ key๋ฅผ index๋ก ์ ๋ฌ๋ฐ์์ ๋ default ๊ฐ์ value๋ก ๋งค์นญ์ํจ๋ค.
์ด๋ฌํ ๊ธฐ๋ฅ์ด ์ ์ฉํ ์ํฉ์ ์ฌ์ฉํ๋ฉด ์ข๋ค.
default๊ฐ์ defaultdictํจ์์ ์ธ์๋ก int ๋ฅผ ์ ๋ฌํ๋ฉด 0, list๋ฅผ ์ ๋ฌํ๋ฉด ๋น ๋ฆฌ์คํธ, set์ ์ ๋ฌํ๋ฉด ๋น ์งํฉ์ผ๋ก ์ ํ๋ค.
from collections import defaultdict
dic_default_zero = defaultdict(int)
๋น ๋ฅธ์ ๋ ฅ
1
2
3
4
|
import sys
def FastInput():
a = sys.stdin.readline().rstrip()
return a
|
cs |
์ ๋ ฅ๋ฐ์ ์์ด ๋ฌด์ง๋ฌด์ง ๋ง์๋ ๊ผญ ์จ์ผํ๋ ๋น ๋ฅธ์ ๋ ฅํจ์.
์ฌ๊ทํจ์ ์ ํ ํด์
import sys
sys.setrecursionlimit(2500)
2500ํ๋ก ์ฌ๊ท ์ ํ์ ํด์ ํ๋ ์ฝ๋์ด๋ค.
๋ฌดํ์ ํํ
๋ฌธ์ ์์ ์ ํ๋ ์ ์ด์์ ํฐ ์๋ฅผ ๋ฌดํ์ ํํํ ๋ณ์์ ๋ด์์ฃผ๋ฉด ๋๋ค.
๋ณดํต ๋ค์๊ณผ ๊ฐ์ ๋ฌธ๋ฒ์ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
INF = int(1e9)
๋ฌธ์์ด ๋ค์ง๊ธฐ
string = string[::-1]
[start : stop : step] ์์ ํ์ฉํ๋ฉด ๋๋ค.
๋ฌธ์์ด์ ๋ฆฌ์คํธ์ฒ๋ผ .reverse() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ ๊ธฐ์ตํ์.
์ง์ ๋ณํ
10์ง์๋ฅผ N์ง์๋ก
num = str(num)
num = int(num, N)
int(string, base)๋ฅผ ํ์ฉํ๋ค. base์ ๋ช ์ง์๋ก ๋ฐ๊ฟ์ง๋ฅผ ์์ฐ์๋ก ์ ๋ฌํ๋ฉด๋๋ค.
N์ง์๋ฅผ 10์ง์๋ก
answer=""
while 1:
answer+=str(num % 3)
num = num // 3
if num == 0:
break
answer = int(answer[::-1])
๊ณ์ฐ์ ํตํด N์ง์ ํํ์ ๊ณ์ฐํ๋ค.
๋ฌธ์์ด์ ๋ค์ง๋ ํ ํฌ๋์ด ๋ค์ด๊ฐ๋ ๊ฒ์ ์ฃผ๋ชฉํ์.
์์ ํ๋ณ
import math
# ์์ ํ๋ณ ํจ์
def is_prime_number(x):
if x < 2 : return False # 1๋๋ 0 ์ ์์๊ฐ ์๋
for i in range(2, int(math.sqrt(x)) + 1):# 2๋ถํฐ x์ ์ ๊ณฑ๊ทผ๊น์ง์ ๋ชจ๋ ์๋ฅผ ํ์ธํ๋ฉฐ
if x % i == 0:# x๊ฐ ํด๋น ์๋ก ๋๋์ด๋จ์ด์ง๋ค๋ฉด
return False # ์์๊ฐ ์๋
return True # ์์์
deepcopy
2์ฐจ์ ์ด์์ ๋ฆฌ์คํธ๋ฅผ ๋ณต์ฌํ ๋ ๋ด์ฉ๋ง ๋ณต์ฌํ๊ณ ์ถ๋ค๋ฉด ๋ฐ๋์ deepcopy๋ฅผ ์ฌ์ฉํ์.
import copy
result = copy.deepcopy(copy_target)
์์ด๊ณผ ์กฐํฉ
items = ['1', '2', '3', '4', '5']
from itertools import permutations
list(permutations(items, 2))
# [('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '1'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '1'), ('3', '2'), ('3', '4'), ('3', '5'), ('4', '1'), ('4', '2'), ('4', '3'), ('4', '5'), ('5', '1'), ('5', '2'), ('5', '3'), ('5', '4')]
from itertools import combinations
list(combinations(items, 2))
# [('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]
๊ธฐ๋ณธ์ ์ธ ์์ด๊ณผ ์กฐํฉ์ ๊ตฌํ๋ ๋ฉ์๋์ด๋ค.
from itertools import product
items = [['a', 'b', 'c,'], ['1', '2', '3', '4'], ['!', '@', '#']]
list(product(*items))
# [('a', '1', '!'), ('a', '1', '@'), ('a', '1', '#'), ('a', '2', '!'), ('a', '2', '@'), ('a', '2', '#'), ('a', '3', '!'), ('a', '3', '@'), ('a', '3', '#'), ('a', '4', '!'), ('a', '4', '@'), ('a', '4', '#'), ('b', '1', '!'), ('b', '1', '@'), ('b', '1', '#'), ('b', '2', '!'), ('b', '2', '@'), ('b', '2', '#'), ('b', '3', '!'), ('b', '3', '@'), ('b', '3', '#'), ('b', '4', '!'), ('b', '4', '@'), ('b', '4', '#'), ('c,', '1', '!'), ('c,', '1', '@'), ('c,', '1', '#'), ('c,', '2', '!'), ('c,', '2', '@'), ('c,', '2', '#'), ('c,', '3', '!'), ('c,', '3', '@'), ('c,', '3', '#'), ('c,', '4', '!'), ('c,', '4', '@'), ('c,', '4', '#')]
๋๊ฐ ์ด์์ ๋ฆฌ์คํธ๋ก ๋ง๋๋ ์กฐํฉ์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋๋ ค์ฃผ๋ ๋ฉ์๋์ด๋ค.