Language/Python

[ํŒŒ์ด์ฌ] ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

meeeeejin 2021. 6. 28. 22:20

 

ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž€ ํŠน์ •ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ‘œ์ค€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•ด ๋†“์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์˜จ๋ผ์ธ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ์„ ํ—ˆ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์ž์ฃผ ์“ฐ์ด๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ๊ณต๋ถ€ํ•ด ๋†“๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 

 

 

์ฃผ์š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • ๋‚ด์žฅ ํ•จ์ˆ˜
    - print(), input() ๋“ฑ์˜ ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ๊ณผ sorted() ๊ฐ™์€ ์ •๋ ฌ ๊ธฐ๋Šฅ ์™ธ ๊ธฐํƒ€ ๋“ฑ๋“ฑ
  • itertools
    - ๋ฐ˜๋ณต๋˜๋Š” ํ˜•ํƒœ(iterable)์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ˆœ์—ด, ์กฐํ•ฉ ๋“ฑ์˜ ๊ธฐ๋Šฅ ์ œ๊ณต
  • heapq
    - ํž™(heap) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ ๊ธฐ๋Šฅ ๊ตฌํ˜„์— ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • bisect
    - ์ด์ง„ ํƒ์ƒ‰(binary search) ๊ธฐ๋Šฅ ์ œ๊ณต
  • collections
    - deque, Counter ๋“ฑ์˜ ์œ ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ ์ œ๊ณต
  • math
    - ํŒฉํ† ๋ฆฌ์–ผ, ์ œ๊ณฑ๊ทผ, ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜(GCD), ์‚ผ๊ฐํ•จ์ˆ˜ ๋“ฑ์˜ ์ˆ˜ํ•™์  ๊ธฐ๋Šฅ ์ œ๊ณต

 

 

๋‹ค์Œ์€ ๊ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฃผ์š” ํ•จ์ˆ˜๋“ค์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์ด๋‹ค. 

 

๋‚ด์žฅ ํ•จ์ˆ˜

# sum(): ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ iterable ๊ฐ์ฒด์˜ ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ ๋ฐ˜ํ™˜
result = sum([1, 2, 3, 4, 5])	# 15

# min(): ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 2๊ฐœ ์ด์ƒ ๋“ค์–ด์™”์„ ๋•Œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋ฐ˜ํ™˜
result = min(7, 3, 5, 2)	# 2

# max(): ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 2๊ฐœ ์ด์ƒ ๋“ค์–ด์™”์„ ๋•Œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜
result = min(7, 3, 5, 2)	# 7

# eval(): ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ๋“ค์–ด์˜จ ์ˆ˜ํ•™ ์ˆ˜์‹์„ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
result = eval("(3 + 5) * 7")	# 56

# sorted(): ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ iterable ๊ฐ์ฒด๋ฅผ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
result = sorted([9, 1, 8, 5, 4])			# [1, 4, 5, 8, 9]
result = sorted([9, 1, 8, 5, 4], reverse = True)	# [9, 8, 5, 4, 1]

# ํŠน์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ์„œ ์ •๋ ฌํ•˜๊ธฐ
# ์ ์ˆ˜์— ๋”ฐ๋ผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
score_list = [('ํ™๊ธธ๋™', 35), ('์ด์ˆœ์‹ ', 75), ('์•„๋ฌด๊ฐœ', 50)]
result = sorted(score_list, key = lambda x: x[1])

 

 

itertools

itertools์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค ์ค‘์— ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋Š” permutations์™€ combinations์ด๋‹ค. 

 

permutations๋Š” iterable ๊ฐ์ฒด์—์„œ r๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์ˆœ์—ด)๋ฅผ ๊ณ„์‚ฐํ•ด์ค€๋‹ค. 

# data ๊ฐ์ฒด์—์„œ 3๊ฐœ์˜ ์›์†Œ๋ฅผ ๋ฝ‘์•„ ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์ˆœ์„œO ์ค‘๋ณตX)
from itertools import permutations

data = ['A', 'B', 'C']
result = list(permutations(data, 3))
# [('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]

 

combinations๋Š” iterable ๊ฐ์ฒด์—์„œ r๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์กฐํ•ฉ)๋ฅผ ๊ณ„์‚ฐํ•ด์ค€๋‹ค. 

# data ๊ฐ์ฒด์—์„œ 2๊ฐœ์˜ ์›์†Œ๋ฅผ ๋ฝ‘๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์ˆœ์„œX ์ค‘๋ณตX)
from itertools import combinations

data = ['A', 'B', 'C']
result = list(combinations(data, 2))
# [('A', 'B'), ('A', 'C'), ('B', 'C')]

 

product๋Š” permutations์™€ ๊ฐ™์ด iterable ๊ฐ์ฒด์—์„œ r๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์ˆœ์—ด)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ๋‹ค๋งŒ ๋‹ค๋ฅธ ์ ์€ product๋Š” ๋ฝ‘๋Š” ์›์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ๋‹ค. 

# data ๊ฐ์ฒด์—์„œ 2๊ฐœ์˜ ์›์†Œ๋ฅผ ๋ฝ‘์•„ ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์ˆœ์„œO ์ค‘๋ณตO)
from itertools import product

data = ['A', 'B', 'C']
result = list(product(data, repeat = 2))
# [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]

 

combinations_with_replacement๋Š” combinations์™€ ๊ฐ™์ด iterable ๊ฐ์ฒด์—์„œ r๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ๋‚˜์—ดํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์กฐํ•ฉ)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ๋‹ค๋งŒ ๋‹ค๋ฅธ ์ ์€ ์›์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ๋‹ค. 

# data ๊ฐ์ฒด์—์„œ 2๊ฐœ์˜ ์›์†Œ๋ฅผ ๋ฝ‘๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ(์ˆœ์„œX ์ค‘๋ณตO)
from itertools import combinations_with_replacement

data = ['A', 'B', 'C']
result = list(combinations_with_replacement(data, 2))
# [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

 

 

heapq

heapq ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํž™(heap) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ํž™์— ์›์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ๋Š” heapq.heappush()๋ฅผ ์ด์šฉํ•˜๊ณ , ํž™์—์„œ ์›์†Œ๋ฅผ ๊บผ๋‚ด๊ณ ์ž ํ•  ๋•Œ๋Š” heapq.heappop()์„ ์ด์šฉํ•œ๋‹ค. ๋‹ค์Œ์€ ํž™ ์ •๋ ฌ(heap sort)๋ฅผ ๊ตฌํ˜„ํ•œ ์˜ˆ์‹œ์ด๋‹ค. 

import heapq

def heapsort(iterable):
  h = []
  result = []
  # ๋ชจ๋“  ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ํž™์— ์‚ฝ์ž…
  for value in iterable:
    heapq.heappush(h, value)
  # ํž™์— ์‚ฝ์ž…๋œ ๋ชจ๋“  ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊บผ๋‚ด๊ธฐ
  for _ in range(len(h)):
    result.append(heapq.heappop(h))
  return result

result = heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
print(result) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ณ„๋„๋กœ ์ตœ๋Œ€ ํž™(max heap)์„ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ตœ๋Œ€ ํž™์„ ๊ตฌํ˜„ํ•  ๋•Œ ์›์†Œ์˜ ๋ถ€ํ˜ธ๋ฅผ ์ž„์‹œ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ตœ๋Œ€ ํž™์„ ์ด์šฉํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ ํž™ ์ •๋ ฌ์„ ๊ตฌํ˜„ํ•œ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

import heapq

# ์ตœ๋Œ€ ํž™์„ ์ด์šฉํ•œ ๋‚ด๋ฆผ์ฐจ์ˆœ ํž™ ์ •๋ ฌ
def heapsort(iterable):
  h = []
  result = []
  # ๋ชจ๋“  ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ํž™์— ์‚ฝ์ž…
  for value in iterable:
    heapq.heappush(h, -value)
  # ํž™์— ์‚ฝ์ž…๋œ ๋ชจ๋“  ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊บผ๋‚ด๊ธฐ
  for _ in range(len(h)):
    result.append(-heapq.heappop(h))
  return result

result = heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
print(result) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

 

 

bisect

bisect ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ํŠน์ •ํ•œ ์›์†Œ๋ฅผ ์ฐพ์•„์•ผ ํ•  ๋•Œ ํšจ๊ณผ์ ์ด๋‹ค. bisect_left()์™€ bisect_right()๋Š” ์‹œ๊ฐ„ ๋ณต์žก๋„ O(logN)์— ๋™์ž‘ํ•œ๋‹ค. 

from bisect import bisect_left, bisect_right

a = [1, 2, 4, 4, 8]
x = 4

# ์ •๋ ฌ๋œ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ a์— ๋ฐ์ดํ„ฐ x๋ฅผ ์‚ฝ์ž…ํ•  ๊ฐ€์žฅ ์™ผ์ชฝ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
bisect_left(a, x)  # 2
# ์ •๋ ฌ๋œ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ a์— ๋ฐ์ดํ„ฐ x๋ฅผ ์‚ฝ์ž…ํ•  ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
bisect_right(a, x) # 4

 

# ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์ด ํŠน์ • ๋ฒ”์œ„์— ์†ํ•˜๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
from bisect import bisect_left, bisect_right

# ๊ฐ’์ด [l_value, r_value]์ธ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def count_by_range(a, l_value, r_value):
  l_index = bisect_left(a, l_value)
  r_index = bisect_right(a, r_value)
  
  return r_index - l_index

 

 

collections

collections์—์„œ ์ œ๊ณตํ•˜๋Š” deque๋Š” ํ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๊ทธ๋ฐ–์— ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” Counter ๋“ฑ์„ ์ œ๊ณตํ•œ๋‹ค. 

# deque๋ฅผ ์ด์šฉํ•œ ์›์†Œ ์‚ฝ์ž… ์‚ญ์ œ
from collections import deque

data = deque([2, 3, 4])
data.appendleft(1)  # ์ œ์ผ ์™ผ์ชฝ์— 1 ์‚ฝ์ž…
data.append(5)  # ์ œ์ผ ์˜ค๋ฅธ์ชฝ์— 5 ์‚ฝ์ž…
data.pop()  # ์ œ์ผ ์˜ค๋ฅธ์ชฝ ์›์†Œ(5) ์‚ญ์ œ
data.popleft()  # ์ œ์ผ ์™ผ์ชฝ ์›์†Œ(1) ์‚ญ์ œ
# Counter ๊ฐ„๋‹จ ์‚ฌ์šฉ๋ฒ•
from collections import Counter

data = ['red', 'blue', 'red', 'green', 'blue', 'blue']
counter = Counter(data)
dict(counter) # {'red': 2, 'blue': 3, 'green': 1}

 

 

math

from math

math.factorial(x) # x์˜ ํŒฉํ† ๋ฆฌ์–ผ ๋ฐ˜ํ™˜
math.sqrt(x)  # x์˜ ์ œ๊ณฑ๊ทผ ๋ฐ˜ํ™˜
math.gcd(a, b)  # a์™€ b์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜ ๋ฐ˜ํ™˜

 

 

 

์ฐธ๊ณ  ์ž๋ฃŒ: ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with Python (๋‚˜๋™๋นˆ ์ €, ํ•œ๋น›๋ฏธ๋””์–ด)

 

 

 

728x90