Test random input for bubble sort (#2492)

This commit is contained in:
Du Yuanchao 2020-10-02 13:55:58 +08:00 committed by GitHub
parent 2388bf4e17
commit 9b3f7c36d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,18 +8,24 @@ def bubble_sort(collection):
Examples:
>>> bubble_sort([0, 5, 2, 3, 2])
[0, 2, 2, 3, 5]
>>> bubble_sort([])
[]
>>> bubble_sort([-2, -45, -5])
[-45, -5, -2]
>>> bubble_sort([-23, 0, 6, -4, 34])
[-23, -4, 0, 6, 34]
>>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
True
>>> bubble_sort([]) == sorted([])
True
>>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5])
True
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
True
>>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
True
>>> import random
>>> collection = random.sample(range(-50, 50), 100)
>>> bubble_sort(collection) == sorted(collection)
True
>>> import string
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
>>> bubble_sort(collection) == sorted(collection)
True
"""
length = len(collection)
for i in range(length - 1):
@ -34,8 +40,11 @@ def bubble_sort(collection):
if __name__ == "__main__":
import doctest
import time
doctest.testmod()
user_input = input("Enter numbers separated by a comma:").strip()
unsorted = [int(item) for item in user_input.split(",")]
start = time.process_time()