Skip to content
Open
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions data_structures/arrays/max_and_min.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Find the maximum and minimum elements in a list.
Source: https://en.wikipedia.org/wiki/Maximum_and_minimum

>>> find_max_min([4, 2, 9, 1, 7])
(9, 1)
>>> find_max_min([-5, -10, 0, 5])
(5, -10)
>>> find_max_min([42])
(42, 42)
>>> find_max_min([])
"""

def find_max_min(arr):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide return type hint for the function: find_max_min. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: arr

"""
Returns the maximum and minimum elements of a list.

Parameters:
arr (list): The list of numbers.

Returns:
tuple: (maximum, minimum)

Raises:
ValueError: If the list is empty.
"""

if not arr:
raise ValueError("find_max_min() arg is an empty list")

Check failure on line 30 in data_structures/arrays/max_and_min.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/max_and_min.py:30:1: W293 Blank line contains whitespace
maximum = max(arr)
minimum = min(arr)
return maximum, minimum


if __name__ == "__main__":
examples = [
[4, 2, 9, 1, 7],
[-5, -10, 0, 5],
[42],
]

for arr in examples:
max_val, min_val = find_max_min(arr)
print(f"For list {arr}, maximum: {max_val}, minimum: {min_val}")

Check failure on line 45 in data_structures/arrays/max_and_min.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

data_structures/arrays/max_and_min.py:45:73: W292 No newline at end of file
Loading