: int = 2 ans : int = 1 while d <= a and d <= b: if a % d == 0 and b % d == 0: a = a // d b = b // d ans = ans * d else: d = d + 1 return ans アルゴリズムとデータ構造 19
1 24 def main(): n : int = int(input("n = ")) s : int = 0 while n > 1 : n = n // 2 s = s + n print(f"s = {s}") 正の整数 に対し、繰り返し回数は 回になる ループ終了時の判定などを省けば ※ オーダー表記では の基数も無視して表記する 。よって計算量は
n-1 n(n-1)/2 n(n-1)/2 n(n-1)/2 def insertion_sort(data: list[int]): n: int = len(data) for i in range(1, n): j: int = i - 1 while j >= 0 and data[j] > data[j + 1]: data[j], data[j + 1] = data[j + 1], data[j] j -= 1 の回数は、 data に依存する 最初からソートされている場合は、 while で終わる
_ は全部マッチングする(それ以外の意味で最後につかう) ガード条件を使ってこんな風にも書けます match def fib(n: int) -> int: match n: case x if x <= 1: return x case _: return fib(n - 1) + fib(n - 2) 再帰関数 44
quick_sort(data: list[int], left: int, right: int): if right - left <= 1: return pivot_index: int = partition(data, left, right) quick_sort(data, left, pivot_index) quick_sort(data, pivot_index + 1, right) left pivot_index right left pivot_index + 1 pivot_index right
int: pivot_index: int = right - 1 pivot: int = data[pivot_index] right = right - 1 while left < right: while left < right and data[left] < pivot: left += 1 while left < right and data[right - 1] >= pivot: right -= 1 if left < right: data[left], data[right - 1] = data[right - 1], data[left] data[pivot_index], data[left] = data[left], data[pivot_index] return left 56
list[int], i: int, j: int, k: int) -> tuple[int, int]: x, y, z = data[i], data[j], data[k] if x <= y <= z or z <= y <= x: return y, j if y <= x <= z or z <= x <= y: return x, i return z, k 59
在位置) def merge_list(data: list[int], left: int, mid: int, right: int): p : int = left q : int = mid working : list[int] = [0] * (right - left) s : int = 0 while p < mid and q < right: if data[p] < data[q]: working[s] = data[p] p += 1 else: working[s] = data[q] q += 1 s += 1 64
: int = 0 hi : int = len(data) while lo < hi: mid : int = (lo + hi) // 2 if data[mid] == key: return mid elif data[mid] > key: hi = mid else: lo = mid + 1 return None 探索 | 二分探索 79
: int = hash_func(key) insert_index : int | None = None for _ in range(HASH_SIZE): match hash_table[index]: case value if value == key: return True case Marker.EMPTY: if insert_index is None: insert_index = index break case Marker.DELETED: if insert_index is None: insert_index = index index = (index + 1) % HASH_SIZE if insert_index is None: return False else: hash_table[insert_index] = key return True 探索 | ハッシュ探索 89
: int = hash_func(key) for _ in range(HASH_SIZE): match hash_table[index]: case value if value == key: return True case Marker.EMPTY: return False index = (index + 1) % HASH_SIZE return False 探索 | ハッシュ探索 90
: int = hash_func(key) for _ in range(HASH_SIZE): match hash_table[index]: case value if value == key: hash_table[index] = Marker.DELETED return True case Marker.EMPTY: return False index = (index + 1) % HASH_SIZE return False 探索 | ハッシュ探索 91
int | None: n : int = len(queue.data) if queue.count <= 0: return None value : int = queue.data[queue.head] queue.head = (queue.head + 1) % n queue.count -= 1 return value データ構造 | キュー 122
target ノードの次のノードを head ノードにする def pop_front(lst: LinkedList) -> int | None: if lst.head is None: return None target = lst.head lst.head = target.nextNode if lst.head is None: lst.tail = None return target.value head target tail head target tail
1 番目のノードを得る new_node の次を index - 1 番目のノードの次のノードにする index - 1 番目のノードの次のノードを new_node にする index new_node head new_node tail index - 1 index head tail index - 1 index
1 番目のノードを得る index - 1 番目のノードの次のノードを target にする index - 1 番目のノードの次のノードを target の次のノードにする index head tail head tail target index - 1 target index + 1 index - 1 index + 1
def merge_list(data: list[int], left: int, mid: int, right: int): p : int = left q : int = mid working : list[int] = list() while p < mid and q < right: if data[p] < data[q]: working.append(data[p]) p += 1 else: working.append(data[q]) q += 1 # 余った要素の追加とdataへの書き戻しは省略 コメント 162