bisect1
| 投稿者 | 桜木凛 |
|---|---|
| 言語 | PyPy3 |
| 更新日時 (JST) | 2026-10-20 01:33:08 |
| サイズ | 1,374 Byte |
| 参照URL | |
日時はサーバーが記録したものです。更新すると更新日時が変わるので、不都合がある場合は新しく投稿してください。
import bisect
from typing import List
def index(a: List[int], x: int) -> int: # 探索したい数値のindexを探索
'Locate the leftmost value exactly equal to x'
i: int = bisect.bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError("value not found")
def find_lt(a: List[int], x: int) -> int: # 探索したい数値未満のうち最大の数値を探索
'Find rightmost value less than x'
i: int = bisect.bisect_left(a, x)
if i:
return a[i-1]
raise ValueError("no item < x")
def find_le(a: List[int], x: int) -> int: # 探索したい数値以下のうち最大の数値を探索
'Find rightmost value less than or equal to x'
i: int = bisect.bisect_right(a, x)
if i:
return a[i-1]
raise ValueError("no item <= x")
def find_gt(a: List[int], x: int) -> int: # 探索したい数値を超えるもののうち最小の数値を探索
'Find leftmost value greater than x'
i: int = bisect.bisect_right(a, x)
if i != len(a):
return a[i]
raise ValueError("no item > x")
def find_ge(a: List[int], x: int) -> int: # 探索したい数値以上のうち最小の数値を探索
'Find leftmost item greater than or equal to x'
i: int = bisect.bisect_left(a, x)
if i != len(a):
return a[i]
raise ValueError("no item >= x")