Skip to content

Commit 28eae42

Browse files
committed
Added annotations to some sorting files
1 parent 678dedb commit 28eae42

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

sorts/bogo_sort.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import random
1717

1818

19-
def bogo_sort(collection):
19+
def bogo_sort(collection: list[int]) -> list[int]:
2020
"""Pure implementation of the bogosort algorithm in Python
2121
:param collection: some mutable ordered collection with heterogeneous
2222
comparable items inside
@@ -30,7 +30,7 @@ def bogo_sort(collection):
3030
[-45, -5, -2]
3131
"""
3232

33-
def is_sorted(collection):
33+
def is_sorted(collection: list[int]) -> bool:
3434
for i in range(len(collection) - 1):
3535
if collection[i] > collection[i + 1]:
3636
return False

sorts/circle_sort.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010

1111

12-
def circle_sort(collection: list) -> list:
12+
def circle_sort(collection: list[int]) -> list[int]:
1313
"""A pure Python implementation of circle sort algorithm
1414
1515
:param collection: a mutable collection of comparable items in any order
@@ -30,7 +30,7 @@ def circle_sort(collection: list) -> list:
3030
if len(collection) < 2:
3131
return collection
3232

33-
def circle_sort_util(collection: list, low: int, high: int) -> bool:
33+
def circle_sort_util(collection: list[int], low: int, high: int) -> bool:
3434
"""
3535
>>> arr = [5,4,3,2,1]
3636
>>> circle_sort_util(lst, 0, 2)

sorts/cycle_sort.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
Source: https://en.wikipedia.org/wiki/Cycle_sort
44
"""
55

6+
from typing import TypeVar
67

7-
def cycle_sort(array: list) -> list:
8+
T = TypeVar('T', int, float)
9+
10+
def cycle_sort(array: list[T]) -> list[T]:
811
"""
912
>>> cycle_sort([4, 3, 2, 1])
1013
[1, 2, 3, 4]
@@ -51,3 +54,5 @@ def cycle_sort(array: list) -> list:
5154
if __name__ == "__main__":
5255
assert cycle_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
5356
assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
57+
assert cycle_sort([-.1, -.2, 1.3, -.8]) == [-0.8, -0.2, -0.1, 1.3]
58+
print("All tests passed")

sorts/merge_sort.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212

13-
def merge_sort(collection: list) -> list:
13+
def merge_sort(collection: list[int]) -> list[int]:
1414
"""
1515
Sorts a list using the merge sort algorithm.
1616
@@ -29,7 +29,7 @@ def merge_sort(collection: list) -> list:
2929
[-45, -5, -2]
3030
"""
3131

32-
def merge(left: list, right: list) -> list:
32+
def merge(left: list[int], right: list[int]) -> list[int]:
3333
"""
3434
Merge two sorted lists into a single sorted list.
3535

sorts/odd_even_sort.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77

8-
def odd_even_sort(input_list: list) -> list:
8+
def odd_even_sort(input_list: list[int]) -> list[int]:
99
"""
1010
Sort input with odd even sort.
1111

0 commit comments

Comments
 (0)