Created
April 25, 2022 10:30
-
-
Save ma7dev/76496e16d04abfa603b1b405c9057a8c to your computer and use it in GitHub Desktop.
Testing sorted()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def naive_way_of_sorting(arr): | |
for i in range(len(arr)): | |
for j in range(i+1, len(arr)): | |
if arr[i] > arr[j]: | |
arr[i], arr[j] = arr[j], arr[i] | |
return arr | |
def test_sorting(size): | |
arr = [random.randint(0, size) for _ in range(size)] | |
# naive implementation that I know forsure it works | |
expected_sorted_arr = naive_way_of_sorting(arr) | |
# the targeted implementation to be compared to | |
actual_sorted_arr = sorted(arr) | |
# comparing expectation vs. actual value | |
assert expected_sorted_arr == actual_sorted_arr | |
test_cases = [0, 1, 10, 100, 1000, 10000] | |
for size in test_cases: | |
print('Testing sorting for size:', size) | |
test_sorting(size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment