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+ text = "ABAABA"
27+ pattern = "AB"
28+ bms = BoyerMooreSearch(text, pattern)
29+ positions = bms.bad_character_heuristic()
30+
31+ where 'positions' contain the locations where the pattern was matched.
32+ """
33+
2534 def __init__ (self , text : str , pattern : str ):
2635 self .text , self .pattern = text , pattern
2736 self .textLen , self .patLen = len (text ), len (pattern )
2837
2938 def match_in_pattern (self , char : str ) -> int :
30- """finds the index of char in pattern in reverse order
39+ """
40+ Finds the index of char in pattern in reverse order.
3141
3242 Parameters :
3343 char (chr): character to be searched
3444
3545 Returns :
3646 i (int): index of char from last in pattern
3747 -1 (int): if char is not found in pattern
48+
49+ >>> bms = BoyerMooreSearch("ABAABA", "AB")
50+ >>> bms.match_in_pattern("B")
51+ 1
3852 """
3953
4054 for i in range (self .patLen - 1 , - 1 , - 1 ):
@@ -44,15 +58,19 @@ def match_in_pattern(self, char: str) -> int:
4458
4559 def mismatch_in_text (self , current_pos : int ) -> int :
4660 """
47- find the index of mis-matched character in text when compared with pattern
48- from last
61+ Find the index of mis-matched character in text when compared with pattern
62+ from last.
4963
5064 Parameters :
5165 current_pos (int): current index position of text
5266
5367 Returns :
5468 i (int): index of mismatched char from last in text
5569 -1 (int): if there is no mismatch between pattern and text block
70+
71+ >>> bms = BoyerMooreSearch("ABAABA", "AB")
72+ >>> bms.mismatch_in_text(2)
73+ 3
5674 """
5775
5876 for i in range (self .patLen - 1 , - 1 , - 1 ):
@@ -61,7 +79,14 @@ def mismatch_in_text(self, current_pos: int) -> int:
6179 return - 1
6280
6381 def bad_character_heuristic (self ) -> list [int ]:
64- # searches pattern in text and returns index positions
82+ """
83+ Finds the positions of the pattern location.
84+
85+ >>> bms = BoyerMooreSearch("ABAABA", "AB")
86+ >>> bms.bad_character_heuristic()
87+ [0, 3]
88+ """
89+
6590 positions = []
6691 for i in range (self .textLen - self .patLen + 1 ):
6792 mismatch_index = self .mismatch_in_text (i )
@@ -75,13 +100,7 @@ def bad_character_heuristic(self) -> list[int]:
75100 return positions
76101
77102
78- text = "ABAABA"
79- pattern = "AB"
80- bms = BoyerMooreSearch (text , pattern )
81- positions = bms .bad_character_heuristic ()
103+ if __name__ == "__main__" :
104+ import doctest
82105
83- if len (positions ) == 0 :
84- print ("No match found" )
85- else :
86- print ("Pattern found in following positions: " )
87- print (positions )
106+ doctest .testmod ()
0 commit comments