MCPcopy
hub / github.com/bregman-arie/devops-exercises / binary_search

Function binary_search

coding/python/binary_search.py:7–19  ·  view source on GitHub ↗

A Binary Search Example which has O(log n) time complexity.

(arr: List[int], lb: int, ub: int, target: int)

Source from the content-addressed store, hash-verified

5
6
7def binary_search(arr: List[int], lb: int, ub: int, target: int) -> Optional[int]:
8 """
9 A Binary Search Example which has O(log n) time complexity.
10 """
11 while lb <= ub:
12 mid = lb + (ub - lb) // 2
13 if arr[mid] == target:
14 return mid
15 elif arr[mid] < target:
16 lb = mid + 1
17 else:
18 ub = mid - 1
19 return -1
20
21
22def generate_random_list(size: int = 10, lower: int = 1, upper: int = 50) -> List[int]:

Callers 1

find_target_in_listFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected