From 8d998a6a2d246e182bec81fae12df57f051e7715 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:16:50 +0530 Subject: [PATCH 1/2] Create stalin_sort.py --- sorts/stalin_sort.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sorts/stalin_sort.py diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py new file mode 100644 index 000000000000..9e0481feee7e --- /dev/null +++ b/sorts/stalin_sort.py @@ -0,0 +1,63 @@ +""" +Stalin Sort algorithm: Removes elements that are out of order. +Elements that are not greater than or equal to the previous element are discarded. +Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420 +""" + +from typing import List + +def stalin_sort(sequence: List[int]) -> List[int]: + """ + Sorts a list using the Stalin sort algorithm. + + Parameters: + sequence (List[int]): A list of non-negative integers. + + Returns: + List[int]: A sorted list according to Stalin Sort. + + Raises: + TypeError: If the sequence contains non-integers or negative integers. + + Examples: + >>> stalin_sort([6, 11, 12, 4, 1, 5]) + [6, 11, 12] + + >>> stalin_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]) + [9] + + >>> stalin_sort([5, 0, 4, 3]) + [5] + + >>> stalin_sort([8, 2, 1]) + [8] + + >>> stalin_sort([1, .9, 0.0, 0, -1, -.9]) + Traceback (most recent call last): + ... + TypeError: Sequence must be a list of non-negative integers + + >>> stalin_sort("Hello world") + Traceback (most recent call last): + ... + TypeError: Sequence must be a list of non-negative integers + """ + if any(not isinstance(x, int) or x < 0 for x in sequence): + raise TypeError("Sequence must be a list of non-negative integers") + + if not sequence: + return [] + result = [sequence[0]] + + for item in sequence[1:]: + if item >= result[-1]: + result.append(item) + + return result + + +if __name__ == "__main__": + + assert stalin_sort([5, 4, 3, 2, 1]) == [5] + assert stalin_sort([7, 9, 4, 3, 5]) == [7, 9] + assert stalin_sort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5] From fab717eb2de81987e44665c3f4d39a806dfadd4f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:48:16 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/stalin_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py index 9e0481feee7e..20a206f94ccb 100644 --- a/sorts/stalin_sort.py +++ b/sorts/stalin_sort.py @@ -6,6 +6,7 @@ from typing import List + def stalin_sort(sequence: List[int]) -> List[int]: """ Sorts a list using the Stalin sort algorithm. @@ -46,7 +47,7 @@ def stalin_sort(sequence: List[int]) -> List[int]: raise TypeError("Sequence must be a list of non-negative integers") if not sequence: - return [] + return [] result = [sequence[0]] for item in sequence[1:]: @@ -57,7 +58,6 @@ def stalin_sort(sequence: List[int]) -> List[int]: if __name__ == "__main__": - assert stalin_sort([5, 4, 3, 2, 1]) == [5] assert stalin_sort([7, 9, 4, 3, 5]) == [7, 9] assert stalin_sort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5]