Skip to content

Commit 51c2f53

Browse files
shriya-97pre-commit-ci[bot]cclauss
authored
add generic list type hints to double_linear_search_recursion (#15489)
* code: add modern list type hint and annotations to double_linear_search_recursion * code: update list type hints to generic Any per code review * code: Add comparable type and person doctests per maintainer review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent ef2ecc4 commit 51c2f53

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎searches/double_linear_search_recursion.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
def search(list_data: list, key: int, left: int = 0, right: int = 0) -> int:
1+
from dataclasses import dataclass
2+
from typing import Protocol
3+
4+
5+
class Comparable(Protocol):
6+
def __eq__(self, other: object) -> bool: ...
7+
def __gt__(self, other: Comparable) -> bool: ...
8+
9+
10+
@dataclass
11+
class Person:
12+
first_name: str
13+
14+
15+
def search[T: Comparable](
16+
list_data: list[T], key: T, left: int = 0, right: int = 0
17+
) -> int:
218
"""
319
Iterate through the array to find the index of key using recursion.
420
:param list_data: the list to be searched
@@ -17,6 +33,10 @@ def search(list_data: list, key: int, left: int = 0, right: int = 0) -> int:
1733
0
1834
>>> search([], 1)
1935
-1
36+
>>> search([Person("Al"), Person("Bob"), Person("Carl")], Person("Bob"))
37+
1
38+
>>> search([Person("Al"), Person("Bob"), Person("Carl")], "Bob")
39+
-1
2040
"""
2141
right = right or len(list_data) - 1
2242
if left > right:

0 commit comments

Comments
 (0)