Skip to content

Commit 4d9b42a

Browse files
committed
Refactor generics to use Python 3.12 type parameter syntax (PEP 695)
1 parent 8f5055c commit 4d9b42a

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

sorts/tim_sort.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from typing import TypeVar
2-
3-
T = TypeVar("T", int, float, str)
4-
5-
def binary_search(lst: list[T], item: T, start: int, end: int) -> int:
1+
def binary_search[T: (int, float, str)](
2+
lst: list[T],
3+
item: T,
4+
start: int,
5+
end: int,
6+
) -> int:
67
if start == end:
78
return start if lst[start] > item else start + 1
89
if start > end:
@@ -17,7 +18,7 @@ def binary_search(lst: list[T], item: T, start: int, end: int) -> int:
1718
return mid
1819

1920

20-
def insertion_sort(lst: list[T]) -> list[T]:
21+
def insertion_sort[T: (int, float, str)](lst: list[T]) -> list[T]:
2122
length = len(lst)
2223

2324
for index in range(1, length):
@@ -28,7 +29,7 @@ def insertion_sort(lst: list[T]) -> list[T]:
2829
return lst
2930

3031

31-
def merge(left: list[T], right: list[T]) -> list[T]:
32+
def merge[T: (int, float, str)](left: list[T], right: list[T]) -> list[T]:
3233
if not left:
3334
return right
3435

@@ -41,7 +42,9 @@ def merge(left: list[T], right: list[T]) -> list[T]:
4142
return [right[0], *merge(left, right[1:])]
4243

4344

44-
def tim_sort(lst: list[T] | tuple[T, ...] | str) -> list[T] | list[str]:
45+
def tim_sort[T: (int, float, str)](
46+
lst: list[T] | tuple[T, ...] | str,
47+
) -> list[T] | list[str]:
4548
"""
4649
>>> tim_sort("Python")
4750
['P', 'h', 'n', 'o', 't', 'y']

0 commit comments

Comments
 (0)