Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions data_structures/Binary search algo/search_insert_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List

Check failure on line 1 in data_structures/Binary search algo/search_insert_position.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/Binary search algo/search_insert_position.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 1 in data_structures/Binary search algo/search_insert_position.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

data_structures/Binary search algo/search_insert_position.py:1:1: INP001 File `data_structures/Binary search algo/search_insert_position.py` is part of an implicit namespace package. Add an `__init__.py`.


def search_insert(nums: List[int], target: int) -> int:

Check failure on line 4 in data_structures/Binary search algo/search_insert_position.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/Binary search algo/search_insert_position.py:4:25: UP006 Use `list` instead of `List` for type annotation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/Binary search algo/search_insert_position.py, please provide doctest for the function search_insert

"""
Finds the index to insert a target value into a sorted list.

If the target is found, its index is returned. Otherwise, returns the
index where it would be if it were inserted in order.

Args:
nums: A sorted list of integers.
target: The integer to search for.

Returns:
The index for the target value.
"""
left, right = 0, len(nums) - 1

while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return left
40 changes: 40 additions & 0 deletions data_structures/Binary search algo/search_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import List

Check failure on line 1 in data_structures/Binary search algo/search_range.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/Binary search algo/search_range.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 1 in data_structures/Binary search algo/search_range.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

data_structures/Binary search algo/search_range.py:1:1: INP001 File `data_structures/Binary search algo/search_range.py` is part of an implicit namespace package. Add an `__init__.py`.


def search_range(nums: List[int], target: int) -> List[int]:

Check failure on line 4 in data_structures/Binary search algo/search_range.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/Binary search algo/search_range.py:4:51: UP006 Use `list` instead of `List` for type annotation

Check failure on line 4 in data_structures/Binary search algo/search_range.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/Binary search algo/search_range.py:4:24: UP006 Use `list` instead of `List` for type annotation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/Binary search algo/search_range.py, please provide doctest for the function search_range

"""
Finds the first and last position of a given target value in a sorted array.

Args:
nums: A sorted list of integers.
target: The integer to search for.

Returns:
A list containing the starting and ending index of the target.
Returns [-1, -1] if the target is not found.
"""

def find_bound(is_first: bool) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/Binary search algo/search_range.py, please provide doctest for the function find_bound

"""Helper to find the leftmost or rightmost occurrence of the target."""
left, right = 0, len(nums) - 1
bound = -1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
bound = mid
if is_first:
right = mid - 1 # Continue searching on the left
else:
left = mid + 1 # Continue searching on the right
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return bound

first_bound = find_bound(True)
if first_bound == -1:
return [-1, -1]

last_bound = find_bound(False)
return [first_bound, last_bound]
Loading