From 58d61ad7fbd2b8219961a1732fa3d00e14e09d48 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Fri, 11 Oct 2024 02:22:48 +0530 Subject: [PATCH 1/2] Create flashsort.py --- sorts/flashsort.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 sorts/flashsort.py diff --git a/sorts/flashsort.py b/sorts/flashsort.py new file mode 100644 index 000000000000..4005778c4e5d --- /dev/null +++ b/sorts/flashsort.py @@ -0,0 +1,46 @@ +import random +from typing import List + +def bogo_sort(collection: List[int]) -> List[int]: + """Pure implementation of the bogosort algorithm in Python. + Bogosort generates random permutations until it guesses the correct one. + + More info on: https://en.wikipedia.org/wiki/Bogosort + Args: + collection (List[int]): A mutable ordered collection with comparable items. + Returns: + List[int]: The same collection ordered by ascending. + Examples: + >>> bogo_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + >>> bogo_sort([]) + [] + >>> bogo_sort([-2, -5, -45]) + [-45, -5, -2] + Raises: + ValueError: If the input is not a list of integers. + Note: + This is not an efficient sorting algorithm and is mainly used for educational purposes. + For doctests, run the following command: + python -m doctest -v bogo_sort.py + or + python3 -m doctest -v bogo_sort.py + + For manual testing, run: + python bogo_sort.py + """ + + def is_sorted(collection: List[int]) -> bool: + for i in range(len(collection) - 1): + if collection[i] > collection[i + 1]: + return False + return True + + while not is_sorted(collection): + random.shuffle(collection) + return collection + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(bogo_sort(unsorted)) From 072f325a03ec006a8f27d037856d452e397ed428 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 20:56:31 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/flashsort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sorts/flashsort.py b/sorts/flashsort.py index 4005778c4e5d..b9444d649443 100644 --- a/sorts/flashsort.py +++ b/sorts/flashsort.py @@ -1,10 +1,11 @@ import random from typing import List + def bogo_sort(collection: List[int]) -> List[int]: """Pure implementation of the bogosort algorithm in Python. Bogosort generates random permutations until it guesses the correct one. - + More info on: https://en.wikipedia.org/wiki/Bogosort Args: collection (List[int]): A mutable ordered collection with comparable items. @@ -25,7 +26,7 @@ def bogo_sort(collection: List[int]) -> List[int]: python -m doctest -v bogo_sort.py or python3 -m doctest -v bogo_sort.py - + For manual testing, run: python bogo_sort.py """ @@ -40,6 +41,7 @@ def is_sorted(collection: List[int]) -> bool: random.shuffle(collection) return collection + if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")]