Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 7fc5f4a

Browse files
committed
Implemented not found behavior
Added custom exception to track negative search results. Updated prefix bisect functions with raise NotFound. Updated find all function.
1 parent 1100d3b commit 7fc5f4a

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/bisearch/prefix.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import List
22

33

4+
class NotFound(Exception):
5+
"""Raised in case the search function failed to find requested value"""
6+
7+
48
def bisect_right(origin: List[str], search: str = "") -> int:
59
"""Return the most right index
610
@@ -12,6 +16,8 @@ def bisect_right(origin: List[str], search: str = "") -> int:
1216
:return: the index of the first string starting with search prefix
1317
:rtype: int
1418
19+
:raise: NotFound
20+
1521
"""
1622

1723
low: int = 0
@@ -27,6 +33,9 @@ def bisect_right(origin: List[str], search: str = "") -> int:
2733
else:
2834
high = idx
2935

36+
if low >= len(origin) and not origin[-1].startswith(search):
37+
raise NotFound(f"'{search}'")
38+
3039
return low
3140

3241

@@ -41,6 +50,8 @@ def bisect_left(origin: List[str], search: str = "") -> int:
4150
:return: the index after the last string starting with search prefix
4251
:rtype: int
4352
53+
:raise: NotFound
54+
4455
"""
4556

4657
low: int = 0
@@ -56,11 +67,29 @@ def bisect_left(origin: List[str], search: str = "") -> int:
5667
else:
5768
high = idx
5869

70+
if low >= len(origin):
71+
raise NotFound(f"'{search}'")
72+
5973
return low
6074

6175

6276
def find_all(origin: List[str], search: str = "") -> List[str]:
63-
start = bisect_left(origin, search)
64-
end = bisect_right(origin, search)
77+
"""Return strings starting with prefix
78+
79+
:param origin: the list of strings
80+
:type origin: list[str]
81+
:param search: the prefix to search, defaults to empty string
82+
:type search: str
83+
84+
:return: the list of strings starting with the search prefix
85+
:rtype: list[str]
86+
"""
87+
88+
try:
89+
start = bisect_left(origin, search)
90+
end = bisect_right(origin, search)
91+
92+
return origin[start:end]
6593

66-
return origin[start:end]
94+
except NotFound:
95+
return []

tests/bisearch/test_prefix.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import pytest
2+
13
from bisearch import prefix
4+
from bisearch.prefix import NotFound
25

36

47
def test_prefix_right(strings_fixtures, pr_fixture):
@@ -28,5 +31,15 @@ def test_find_all_default(strings_small):
2831
assert prefix.find_all(strings_small) == strings_small
2932

3033

34+
def test_prefix_right_negative(strings_small):
35+
with pytest.raises(NotFound):
36+
prefix.bisect_right(strings_small, "d")
37+
38+
39+
def test_prefix_left_negative(strings_small):
40+
with pytest.raises(NotFound):
41+
prefix.bisect_left(strings_small, "d")
42+
43+
3144
def test_find_all_negative(strings_small):
3245
assert prefix.find_all(strings_small, "d") == []

0 commit comments

Comments
 (0)