Skip to content
Closed
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
25 changes: 25 additions & 0 deletions data_structures/arrays/peak_mountain_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
peak_mountain_array.py
Author: Taha Zulfiquar
Description: Finds the peak index in a mountain array using binary search.
"""

from typing import List

Check failure on line 7 in data_structures/arrays/peak_mountain_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/peak_mountain_array.py:7:1: UP035 `typing.List` is deprecated, use `list` instead


def peak_mountain(arr: List[int]) -> int:

Check failure on line 10 in data_structures/arrays/peak_mountain_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/peak_mountain_array.py:10:24: UP006 Use `list` instead of `List` for type annotation
Copy link
Copy Markdown

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/arrays/peak_mountain_array.py, please provide doctest for the function peak_mountain

start, end = 0, len(arr) - 1

while start < end:
mid = start + (end - start) // 2
if arr[mid] > arr[mid + 1]:
end = mid
else:
start = mid + 1
return start


if __name__ == "__main__":
arr = [1, 2, 3, 5, 7, 8, 6, 3, 2]
peak_index = peak_mountain(arr)
print(f"The peak index is: {peak_index}")
Loading