1313
1414from __future__ import annotations
1515
16+ from typing import Any , Protocol
1617
17- def binary_search_insertion (sorted_list , item ):
18+
19+ class Comparable (Protocol ):
20+ def __lt__ (self , other : Any , / ) -> bool : ...
21+
22+
23+ def binary_search_insertion [T : Comparable ](sorted_list : list [T ], item : T ) -> list [T ]:
1824 """
1925 >>> binary_search_insertion([1, 2, 7, 9, 10], 4)
2026 [1, 2, 4, 7, 9, 10]
@@ -35,7 +41,7 @@ def binary_search_insertion(sorted_list, item):
3541 return sorted_list
3642
3743
38- def merge (left , right ) :
44+ def merge [ T : Comparable ] (left : list [ list [ T ]] , right : list [ list [ T ]]) -> list [ list [ T ]] :
3945 """
4046 >>> merge([[1, 6], [9, 10]], [[2, 3], [4, 5], [7, 8]])
4147 [[1, 6], [2, 3], [4, 5], [7, 8], [9, 10]]
@@ -49,7 +55,7 @@ def merge(left, right):
4955 return result + left + right
5056
5157
52- def sortlist_2d (list_2d ) :
58+ def sortlist_2d [ T : Comparable ] (list_2d : list [ list [ T ]]) -> list [ list [ T ]] :
5359 """
5460 >>> sortlist_2d([[9, 10], [1, 6], [7, 8], [2, 3], [4, 5]])
5561 [[1, 6], [2, 3], [4, 5], [7, 8], [9, 10]]
@@ -61,7 +67,7 @@ def sortlist_2d(list_2d):
6167 return merge (sortlist_2d (list_2d [:middle ]), sortlist_2d (list_2d [middle :]))
6268
6369
64- def merge_insertion_sort (collection : list [int ]) -> list [int ]:
70+ def merge_insertion_sort [ T : Comparable ] (collection : list [T ]) -> list [T ]:
6571 """Pure implementation of merge-insertion sort algorithm in Python
6672
6773 :param collection: some mutable ordered collection with heterogeneous
0 commit comments