1111a shift is proposed that moves the entirety of Pattern past
1212the point of mismatch in the text.
1313
14- If there no mismatch then the pattern matches with text block.
14+ If there is no mismatch then the pattern matches with text block.
1515
1616Time Complexity : O(n/m)
1717 n=length of main string
1818 m=length of pattern string
1919"""
2020
21- from __future__ import annotations
22-
2321
2422class BoyerMooreSearch :
23+ """
24+ Example usage:
25+
26+ bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
27+ positions = bms.bad_character_heuristic()
28+
29+ where 'positions' contain the locations where the pattern was matched.
30+ """
31+
2532 def __init__ (self , text : str , pattern : str ):
2633 self .text , self .pattern = text , pattern
2734 self .textLen , self .patLen = len (text ), len (pattern )
2835
2936 def match_in_pattern (self , char : str ) -> int :
30- """finds the index of char in pattern in reverse order
37+ """
38+ Finds the index of char in pattern in reverse order.
3139
3240 Parameters :
3341 char (chr): character to be searched
3442
3543 Returns :
3644 i (int): index of char from last in pattern
3745 -1 (int): if char is not found in pattern
46+
47+ >>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
48+ >>> bms.match_in_pattern("B")
49+ 1
3850 """
3951
4052 for i in range (self .patLen - 1 , - 1 , - 1 ):
@@ -44,15 +56,19 @@ def match_in_pattern(self, char: str) -> int:
4456
4557 def mismatch_in_text (self , current_pos : int ) -> int :
4658 """
47- find the index of mis-matched character in text when compared with pattern
48- from last
59+ Find the index of mis-matched character in text when compared with pattern
60+ from last.
4961
5062 Parameters :
5163 current_pos (int): current index position of text
5264
5365 Returns :
5466 i (int): index of mismatched char from last in text
5567 -1 (int): if there is no mismatch between pattern and text block
68+
69+ >>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
70+ >>> bms.mismatch_in_text(2)
71+ 3
5672 """
5773
5874 for i in range (self .patLen - 1 , - 1 , - 1 ):
@@ -61,7 +77,14 @@ def mismatch_in_text(self, current_pos: int) -> int:
6177 return - 1
6278
6379 def bad_character_heuristic (self ) -> list [int ]:
64- # searches pattern in text and returns index positions
80+ """
81+ Finds the positions of the pattern location.
82+
83+ >>> bms = BoyerMooreSearch(text="ABAABA", pattern="AB")
84+ >>> bms.bad_character_heuristic()
85+ [0, 3]
86+ """
87+
6588 positions = []
6689 for i in range (self .textLen - self .patLen + 1 ):
6790 mismatch_index = self .mismatch_in_text (i )
@@ -75,13 +98,7 @@ def bad_character_heuristic(self) -> list[int]:
7598 return positions
7699
77100
78- text = "ABAABA"
79- pattern = "AB"
80- bms = BoyerMooreSearch (text , pattern )
81- positions = bms .bad_character_heuristic ()
101+ if __name__ == "__main__" :
102+ import doctest
82103
83- if len (positions ) == 0 :
84- print ("No match found" )
85- else :
86- print ("Pattern found in following positions: " )
87- print (positions )
104+ doctest .testmod ()
0 commit comments