Skip to content

Instantly share code, notes, and snippets.

View devbruce's full-sized avatar

Daeyeong Kim devbruce

View GitHub Profile
__all__ = ["find", "union", "is_group"]
def find(node: int, parents: dict[int, int]) -> int:
if parents[node] != node:
parents[node] = find(parents[node], parents=parents)
return parents[node]
def union(n1: int, n2: int, parents: dict[int, int]) -> None:
"""
- Greatest Common Divisor(최대공약수) by Euclidean algorithm(유클리드 호제법)
> Reference: Wiki(https://namu.wiki/w/%EC%9C%A0%ED%81%B4%EB%A6%AC%EB%93%9C%20%ED%98%B8%EC%A0%9C%EB%B2%95)
- Least Common Multiple(최소공배수): 두 수의 곱 / 최대공약수
"""
__all__ = ['gcd_while', 'gcd_recursive', 'lcm']
@devbruce
devbruce / divisors.py
Last active March 5, 2023 01:43
Algorithm: Related to Divisors
"""
Additional Info
- 제곱수(1, 4, 9, 16, 25, 100, 121, ...)의 약수의 갯수는 항상 홀수이다.
"""
__all__ = ['get_divisors', 'get_n_divisors']
@devbruce
devbruce / linalg_funcs.py
Last active July 18, 2022 04:37
Implementation functions of Linear Algebra
import numpy as np
__all__ = ['get_cos_sim', 'apply_svd']
def get_cos_sim(v1:np.ndarray, v2:np.ndarray) -> np.float64:
cos_sim = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
return cos_sim
"""
Good site for sorting algorithm visualization
- https://visualgo.net/en/sorting
"""
__all__ = [
'bubble_sort', 'insertion_sort', 'selection_sort',
'quick_sort', 'merge_sort',
]
@devbruce
devbruce / dfs_recursive.py
Last active February 24, 2024 04:05
DFS with recursive
__all__ = ['dfs_recursive']
visited = set()
visited_orders = list()
def dfs_recursive(start_node: str, graph: dict[str, list[str]]) -> None:
if start_node in visited:
return
@devbruce
devbruce / dfs_bfs.py
Last active June 24, 2022 05:25
Base code of DFS & BFS
"""
* DFS(Depth First Search)
- Used Data Structure: Stack
- Time Complexity: O(V+E)
* BFS(Breadth First Search)
- Used Data Structure: Queue
- Time Complexity: O(V+E)
"""
from collections import deque
@devbruce
devbruce / binary_search.py
Last active December 23, 2023 10:45
Binary search with Python
"""
Binary search must be preconditioned that arr is sorted(desc).
"""
__all__ = ['bin_search', 'binary_search', 'binary_search_recursive', 'binary_search_recursive2']
def bin_search(arr: list[int], target: int) -> int:
"""Ref: https://www.acmicpc.net/blog/view/109
@devbruce
devbruce / nms.py
Last active May 15, 2022 12:25
Non-Maximum Suppression
import numpy as np
__all__ = ['nms']
def nms(pred_boxes, iou_thr=0.7, eps=1e-6):
"""Non-Maximum Suppression
Args:
@devbruce
devbruce / img_letterbox.py
Last active May 15, 2022 12:26
Image Letterboxing (or Pillarboxing)
import cv2
import numpy as np
__all__ = ['letterbox']
def letterbox(img_arr, target_size, boxes=None):
"""Image Letterboxing (or Pillarboxing)