[mypy]Correction of all errors in the sorts directory (#4224)

* [mypy] Add/fix type annotations for recursive_insertion_sort(#4085)

* [mypy] Add/fix type annotations for bucket_sort(#4085)

* [mypy] Reworked code for cocktail_shaker_sort so that missing return statement error is resolved(#4085)

* [mypy] Add/fix type annotations for patience_sort(#4085)

* [mypy] Add/fix type annotations for radix_sort(#4085)

Co-authored-by: goodm2 <4qjpngu8mem8cz>
This commit is contained in:
Matthew 2021-02-23 09:02:30 +00:00 committed by GitHub
parent 02d9bc66c1
commit a4726ca248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 7 deletions

View File

@ -27,6 +27,7 @@ If k = O(n), time complexity is O(n)
Source: https://en.wikipedia.org/wiki/Bucket_sort Source: https://en.wikipedia.org/wiki/Bucket_sort
""" """
from typing import List
def bucket_sort(my_list: list) -> list: def bucket_sort(my_list: list) -> list:
@ -51,7 +52,7 @@ def bucket_sort(my_list: list) -> list:
return [] return []
min_value, max_value = min(my_list), max(my_list) min_value, max_value = min(my_list), max(my_list)
bucket_count = int(max_value - min_value) + 1 bucket_count = int(max_value - min_value) + 1
buckets = [[] for _ in range(bucket_count)] buckets: List[list] = [[] for _ in range(bucket_count)]
for i in range(len(my_list)): for i in range(len(my_list)):
buckets[(int(my_list[i] - min_value) // bucket_count)].append(my_list[i]) buckets[(int(my_list[i] - min_value) // bucket_count)].append(my_list[i])

View File

@ -33,6 +33,7 @@ def cocktail_shaker_sort(unsorted: list) -> list:
swapped = True swapped = True
if not swapped: if not swapped:
break
return unsorted return unsorted

View File

@ -1,6 +1,7 @@
from bisect import bisect_left from bisect import bisect_left
from functools import total_ordering from functools import total_ordering
from heapq import merge from heapq import merge
from typing import List
""" """
A pure Python implementation of the patience sort algorithm A pure Python implementation of the patience sort algorithm
@ -43,7 +44,7 @@ def patience_sort(collection: list) -> list:
>>> patience_sort([-3, -17, -48]) >>> patience_sort([-3, -17, -48])
[-48, -17, -3] [-48, -17, -3]
""" """
stacks = [] stacks: List[Stack] = []
# sort into stacks # sort into stacks
for element in collection: for element in collection:
new_stacks = Stack([element]) new_stacks = Stack([element])

View File

@ -30,7 +30,7 @@ def radix_sort(list_of_ints: List[int]) -> List[int]:
max_digit = max(list_of_ints) max_digit = max(list_of_ints)
while placement <= max_digit: while placement <= max_digit:
# declare and initialize empty buckets # declare and initialize empty buckets
buckets = [list() for _ in range(RADIX)] buckets: List[list] = [list() for _ in range(RADIX)]
# split list_of_ints between the buckets # split list_of_ints between the buckets
for i in list_of_ints: for i in list_of_ints:
tmp = int((i / placement) % RADIX) tmp = int((i / placement) % RADIX)

View File

@ -4,6 +4,8 @@ A recursive implementation of the insertion sort algorithm
from __future__ import annotations from __future__ import annotations
from typing import List
def rec_insertion_sort(collection: list, n: int): def rec_insertion_sort(collection: list, n: int):
""" """
@ -70,6 +72,6 @@ def insert_next(collection: list, index: int):
if __name__ == "__main__": if __name__ == "__main__":
numbers = input("Enter integers separated by spaces: ") numbers = input("Enter integers separated by spaces: ")
numbers = [int(num) for num in numbers.split()] number_list: List[int] = [int(num) for num in numbers.split()]
rec_insertion_sort(numbers, len(numbers)) rec_insertion_sort(number_list, len(number_list))
print(numbers) print(number_list)