Add Boyer-Moore-Horspool string-search algorithm#14630
Open
ChrisJr404 wants to merge 1 commit intoTheAlgorithms:masterfrom
Open
Add Boyer-Moore-Horspool string-search algorithm#14630ChrisJr404 wants to merge 1 commit intoTheAlgorithms:masterfrom
ChrisJr404 wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change
Adds the Boyer-Moore-Horspool string-search algorithm under
strings/boyer_moore_horspool.py.Horspool (1980) is the most widely-taught simplification of the Boyer-Moore family: it keeps only the bad-character shift table and uses the rightmost character of the current text window to drive the shift. It is sub-linear on average (~O(n / m) on random text), worst-case O(n * m), and uses O(sigma) memory where sigma is the size of the alphabet that appears in the pattern. It is often the simplest sub-linear matcher to implement and is the algorithm used internally by libraries such as glibc's
memmemfor short patterns.The repository already contains the original Boyer-Moore (
strings/boyer_moore_search.py) and Knuth-Morris-Pratt (strings/knuth_morris_pratt.py); Horspool is a distinct, well-known variant and was missing. I confirmed the file does not already exist (rg horspoolonly matches the dictionary corpus) andDIRECTORY.mdhad no entry for it.Reference: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm
The module exposes:
boyer_moore_horspool_search(text, pattern) -> int- first-match index,-1on no match, behaves likestr.findfor the empty-pattern edge case.boyer_moore_horspool_search_all(text, pattern) -> list[int]- all (overlapping) match indices._build_shift_table(pattern)- private helper, also doctested.Checklist
Tests
str.find.python -m doctest strings/boyer_moore_horspool.py -v-> 18 passed.python -m pytest strings/boyer_moore_horspool.py --doctest-modules-> 3 passed.str.findand a naive all-occurrences oracle: 100% agreement.pre-commit run --files strings/boyer_moore_horspool.py DIRECTORY.mdpasses (ruff check, ruff format, codespell, auto-walrus, validate-filenames).DIRECTORY.mdupdated alphabetically in the Strings section.