Skip to content

Commit e10000f

Browse files
committed
add problem of generic search
1 parent f7ba0b7 commit e10000f

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ Collections.binarySearch(arrayList, 3); // for list
944944
- Find k-th smallest/largest element in an array (`FindKthSmallestElement`/`FindKthLargestElement`), EPI#11.8: [c++](cpp-algorithm/src/search) | Find the k-th smallest/largest element in an array using the quickselect algorithm (`QuickSelectAlgorithm`).
945945
- Find the minimum and maximum elements in an array (`FindMinMax`), EPI#11.7: [c++](cpp-algorithm/src/search)
946946
- Search a codon(combinations of three nucleotides) in a gene (`linear_contains`, `binary_contains`), CCSP#2.1: [python](python-algorithm/src/search) | Search a codon(combinations of three nucleotides) in a gene using linear search and binary search.
947+
- Search an element in generic list (`generic_linear_contains`, `generic_linear_contains`), CCSP#2.1: [python](python-algorithm/src/search) | Search an element in generic list using linear search and binary search.
947948
- Search a sorted array for entry equal to its index (`SearchEntryEqualToItsIndex`), EPI#11.2: [c++](cpp-algorithm/src/search)
948949
- Search a sorted array for the first greater than a key (`SearchFirstGreaterThanKey`): [c++](cpp-algorithm/src/search)
949950
- Search a sorted array for the first occurrence of a key (`SearchFirstOfKey`), EPI#11.1: [c++](cpp-algorithm/src/search)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import TypeVar, Iterable, Tuple, List, Sequence
2+
3+
T = TypeVar('T')
4+
C = TypeVar("C", bound="Comparable")
5+
6+
7+
def generic_linear_contains(iterable: Iterable[T], key: T) -> bool:
8+
return any(item == key for item in iterable)
9+
10+
11+
def generic_binary_contains(sequence: Sequence[C], key: C) -> bool:
12+
low: int = 0
13+
high: int = len(sequence) - 1
14+
while low <= high:
15+
mid: int = (low + high) // 2
16+
if sequence[mid] < key:
17+
low = mid + 1
18+
elif sequence[mid] > key:
19+
high = mid - 1
20+
else:
21+
return True
22+
return False
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from algorithm.search.generic_search import generic_linear_contains, generic_binary_contains
3+
4+
5+
@pytest.mark.benchmark(group="linear_contains")
6+
@pytest.mark.parametrize("elements, target, expected", [
7+
([1, 5, 15, 15, 15, 15, 20], 5, True),
8+
], ids=["successful"])
9+
def test_generic_linear_contains(benchmark, elements, target, expected):
10+
result = benchmark(generic_linear_contains, elements, target)
11+
assert expected == result
12+
13+
14+
@pytest.mark.benchmark(group="binary_contains")
15+
@pytest.mark.parametrize("elements, target, expected", [
16+
(["a", "d", "e", "f", "z"], "f", True),
17+
(["john", "mark", "ronald", "sarah"], "sheila", False),
18+
], ids=["successful", "failed"])
19+
def test_generic_binary_contains(benchmark, elements, target, expected):
20+
result = benchmark(generic_binary_contains, elements, target)
21+
assert expected == result

0 commit comments

Comments
 (0)