Skip to content

Commit 537aaa9

Browse files
committed
test: add doctest that would infinite-loop with naive shift fix
Addresses reviewer feedback by adding a test case where the mismatched character ('C') appears to the RIGHT of the mismatch offset in the pattern 'ABCB'. A naive shift that searches the entire pattern would compute a negative shift and loop forever; the correct implementation only searches to the left of the mismatch offset, guaranteeing shift>=1. If the shift logic regresses, this doctest will hang instead of passing.
1 parent b55d94a commit 537aaa9

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

strings/boyer_moore_search.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ def bad_character_heuristic(self) -> list[int]:
116116
>>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
117117
>>> bms.bad_character_heuristic()
118118
[0, 3]
119+
120+
The text below contains "CCB" which causes the mismatched character 'C'
121+
to appear to the RIGHT of the mismatch offset inside the pattern "ABCB".
122+
A naive shift (searching the entire pattern) would compute a zero or
123+
negative shift and loop forever; the correct shift only looks to the left
124+
of the mismatch offset, guaranteeing forward progress.
125+
126+
>>> bms = BoyerMooreSearch(text="ABCBACCBABCB", pattern="ABCB")
127+
>>> bms.bad_character_heuristic()
128+
[0, 8]
119129
"""
120130

121131
positions = []

0 commit comments

Comments
 (0)