initial commit

This commit is contained in:
2022-08-30 13:16:48 +02:00
commit 89071996f3
6 changed files with 268 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
import pytest
def binary_search(nums: list[int], target: int):
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
@pytest.mark.parametrize(
"nums,target,expected",
[([*range(5)], 2, 2), ([*range(5)], 6, -1), ([*range(20, 50)], 21, 1)],
)
def test_binary_search(nums: list[int], target: int, expected: int):
assert binary_search(nums, target) == expected