Skip to content

Commit 6f07bcd

Browse files
committed
Refactor: use Sequence[T] with TypeVar instead of list[Any]
As suggested by TTAAAN, using Sequence[T] with TypeVar provides better type consistency and allows the functions to accept tuples as well as lists.
1 parent 9e0ae92 commit 6f07bcd

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

searches/linear_search.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
python3 linear_search.py
99
"""
1010

11-
from typing import Any
11+
from typing import Sequence, TypeVar
1212

13+
T = TypeVar("T")
1314

14-
def linear_search(sequence: list[Any], target: Any) -> int:
15+
16+
def linear_search(sequence: Sequence[T], target: T) -> int:
1517
"""A pure Python implementation of a linear search algorithm
1618
1719
:param sequence: a collection with comparable items (sorting is not required for
@@ -35,7 +37,7 @@ def linear_search(sequence: list[Any], target: Any) -> int:
3537
return -1
3638

3739

38-
def rec_linear_search(sequence: list[Any], low: int, high: int, target: Any) -> int:
40+
def rec_linear_search(sequence: Sequence[T], low: int, high: int, target: T) -> int:
3941
"""
4042
A pure Python implementation of a recursive linear search algorithm
4143

0 commit comments

Comments
 (0)