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
35 changes: 29 additions & 6 deletions sorts/merge_sort.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
This is a pure Python implementation of the merge sort algorithm.
This is a pure Python implementation of the merge sort algorithm
with added input validation for safer usage.

For doctests run following command:
python -m doctest -v merge_sort.py
Expand All @@ -9,6 +10,22 @@
python merge_sort.py
"""

# -----------------------
# Input validation helper
# -----------------------
def _validate_sort_input(arr):
"""
Ensures the input is a list (or tuple) of comparable elements.

Raises:
TypeError: if arr is not a list or tuple.
ValueError: if list contains uncomparable elements.
"""
if not isinstance(arr, (list, tuple)):
raise TypeError("merge_sort() input must be a list or tuple.")
if len(arr) > 1 and not all(isinstance(x, (int, float, str)) for x in arr):
raise ValueError("merge_sort() elements must be comparable (int, float, or str).")

Check failure on line 27 in sorts/merge_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/merge_sort.py:27:89: E501 Line too long (90 > 88)


def merge_sort(collection: list) -> list:
"""
Expand All @@ -28,6 +45,7 @@
>>> merge_sort([-2, -5, -45])
[-45, -5, -2]
"""
_validate_sort_input(collection)

def merge(left: list, right: list) -> list:
"""
Expand All @@ -45,20 +63,25 @@
return result

if len(collection) <= 1:
return collection
return list(collection)

mid_index = len(collection) // 2
return merge(merge_sort(collection[:mid_index]), merge_sort(collection[mid_index:]))
return merge(
merge_sort(collection[:mid_index]),
merge_sort(collection[mid_index:])
)


if __name__ == "__main__":
import doctest

doctest.testmod()

try:
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
unsorted = [int(item) for item in user_input.split(",") if item]
sorted_list = merge_sort(unsorted)
print(*sorted_list, sep=",")
print("Sorted list:", *sorted_list, sep=" ")
except ValueError:
print("Invalid input. Please enter valid integers separated by commas.")
except TypeError as e:
print(e)
Loading