Using Horspool Algorithm
This is a Python implementation of the Horspool string searching algorithm. The Horspool algorithm is used to find occurrences of a pattern string within a given text string. The implementation includes variations of the algorithm to handle different pattern cases, such as wildcard characters and anchor characters.
-
download the code files.
-
Modify the text and pattern input files as needed. You can replace the contents of the
textN.txt
andpatternN.txt
files with your own text and pattern strings. -
Run the
main.py
script to perform the string search and generate output files.python assignment_01.py compiler ask to enter N value as your file name. for example if you need run text5.txt and pattern5.txt set. Then you should input 5.
-
The output files, named
outputN.txt
, will be generated, showing the positions where the pattern was found in the text.
The algorithm includes different variations to handle various pattern cases:
horspool(text, pattern, text_file)
: Basic Horspool algorithm to search for exact pattern matches.horspool_dot(text, pattern, text_file)
: Horspool algorithm with support for the.
wildcard character.horspool_quest(text, pattern, text_file)
: Horspool algorithm to handle patterns containing?
characters.horspool_begg(text, pattern, text_file)
: Horspool algorithm to handle patterns anchored at the beginning.horspool_end(text, pattern, text_file)
: Horspool algorithm to handle patterns anchored at the end.
textN.txt
: ReplaceN
with the specific input number. Contains the text in which the pattern is searched.patternN.txt
: ReplaceN
with the specific input number. Contains the pattern string to be searched.
Output files, named outputN.txt
, are generated for each input pair. These files contain the positions where the pattern was found in the corresponding text.