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
46 changes: 46 additions & 0 deletions data_structures/arrays/max_min.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
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):

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")

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}")
80 changes: 80 additions & 0 deletions data_structures/arrays/rotate_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
def rotate_array(arr: list[int], steps: int) -> list[int]:
"""
Rotates a list to the right by steps positions.

Parameters:
arr (List[int]): The list of integers to rotate.
steps (int): Number of positions to rotate. Can be negative for left rotation.

Returns:
List[int]: Rotated list.

Examples:
>>> rotate_array([1, 2, 3, 4, 5], 2)
[4, 5, 1, 2, 3]
>>> rotate_array([1, 2, 3, 4, 5], -2)
[3, 4, 5, 1, 2]
>>> rotate_array([1, 2, 3, 4, 5], 7)
[4, 5, 1, 2, 3]
>>> rotate_array([], 3)
[]
"""

n = len(arr)
if n == 0:
return arr

steps = steps % n

if steps < 0:
steps += n

def reverse(start: int, end: int) -> None:
"""
Reverses a portion of the list in place from index start to end.

Parameters:
start (int): Starting index of the portion to reverse.
end (int): Ending index of the portion to reverse.

Returns:
None

Examples:
>>> example = [1, 2, 3, 4, 5]
>>> def reverse_test(arr, start, end):
... while start < end:
... arr[start], arr[end] = arr[end], arr[start]
... start += 1
... end -= 1
>>> reverse_test(example, 0, 2)
>>> example
[3, 2, 1, 4, 5]
>>> reverse_test(example, 2, 4)
>>> example
[3, 2, 5, 4, 1]
"""

while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1

reverse(0, n - 1)
reverse(0, steps - 1)
reverse(steps, n - 1)

return arr


if __name__ == "__main__":
examples = [
([1, 2, 3, 4, 5], 2),
([1, 2, 3, 4, 5], -2),
([1, 2, 3, 4, 5], 7),
([], 3),
]

for arr, steps in examples:
rotated = rotate_array(arr.copy(), steps)
print(f"Rotate {arr} by {steps}: {rotated}")
Loading