From 9b3f7c36d0e116fac5df72fa35aa2fe74bb1b927 Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Fri, 2 Oct 2020 13:55:58 +0800 Subject: [PATCH] Test random input for bubble sort (#2492) --- sorts/bubble_sort.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index eb356bc7d..d4f0d25ca 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -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()