Skip to content

Commit 8d76c78

Browse files
MatthewG25shermanhui
authored andcommitted
[mypy]Correction of all errors in the sorts directory (TheAlgorithms#4224)
* [mypy] Add/fix type annotations for recursive_insertion_sort(TheAlgorithms#4085) * [mypy] Add/fix type annotations for bucket_sort(TheAlgorithms#4085) * [mypy] Reworked code for cocktail_shaker_sort so that missing return statement error is resolved(TheAlgorithms#4085) * [mypy] Add/fix type annotations for patience_sort(TheAlgorithms#4085) * [mypy] Add/fix type annotations for radix_sort(TheAlgorithms#4085) Co-authored-by: goodm2 <4qjpngu8mem8cz>
1 parent 25593be commit 8d76c78

5 files changed

+12
-7
lines changed

Diff for: sorts/bucket_sort.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
2828
Source: https://en.wikipedia.org/wiki/Bucket_sort
2929
"""
30+
from typing import List
3031

3132

3233
def bucket_sort(my_list: list) -> list:
@@ -51,7 +52,7 @@ def bucket_sort(my_list: list) -> list:
5152
return []
5253
min_value, max_value = min(my_list), max(my_list)
5354
bucket_count = int(max_value - min_value) + 1
54-
buckets = [[] for _ in range(bucket_count)]
55+
buckets: List[list] = [[] for _ in range(bucket_count)]
5556

5657
for i in range(len(my_list)):
5758
buckets[(int(my_list[i] - min_value) // bucket_count)].append(my_list[i])

Diff for: sorts/cocktail_shaker_sort.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def cocktail_shaker_sort(unsorted: list) -> list:
3333
swapped = True
3434

3535
if not swapped:
36-
return unsorted
36+
break
37+
return unsorted
3738

3839

3940
if __name__ == "__main__":

Diff for: sorts/patience_sort.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from bisect import bisect_left
22
from functools import total_ordering
33
from heapq import merge
4+
from typing import List
45

56
"""
67
A pure Python implementation of the patience sort algorithm
@@ -43,7 +44,7 @@ def patience_sort(collection: list) -> list:
4344
>>> patience_sort([-3, -17, -48])
4445
[-48, -17, -3]
4546
"""
46-
stacks = []
47+
stacks: List[Stack] = []
4748
# sort into stacks
4849
for element in collection:
4950
new_stacks = Stack([element])

Diff for: sorts/radix_sort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def radix_sort(list_of_ints: List[int]) -> List[int]:
3030
max_digit = max(list_of_ints)
3131
while placement <= max_digit:
3232
# declare and initialize empty buckets
33-
buckets = [list() for _ in range(RADIX)]
33+
buckets: List[list] = [list() for _ in range(RADIX)]
3434
# split list_of_ints between the buckets
3535
for i in list_of_ints:
3636
tmp = int((i / placement) % RADIX)

Diff for: sorts/recursive_insertion_sort.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from __future__ import annotations
66

7+
from typing import List
8+
79

810
def rec_insertion_sort(collection: list, n: int):
911
"""
@@ -70,6 +72,6 @@ def insert_next(collection: list, index: int):
7072

7173
if __name__ == "__main__":
7274
numbers = input("Enter integers separated by spaces: ")
73-
numbers = [int(num) for num in numbers.split()]
74-
rec_insertion_sort(numbers, len(numbers))
75-
print(numbers)
75+
number_list: List[int] = [int(num) for num in numbers.split()]
76+
rec_insertion_sort(number_list, len(number_list))
77+
print(number_list)

0 commit comments

Comments
 (0)