11
11
a shift is proposed that moves the entirety of Pattern past
12
12
the point of mismatch in the text.
13
13
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.
15
15
16
16
Time Complexity : O(n/m)
17
17
n=length of main string
18
18
m=length of pattern string
19
19
"""
20
20
21
- from __future__ import annotations
22
-
23
21
24
22
class 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
+
25
32
def __init__ (self , text : str , pattern : str ):
26
33
self .text , self .pattern = text , pattern
27
34
self .textLen , self .patLen = len (text ), len (pattern )
28
35
29
36
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.
31
39
32
40
Parameters :
33
41
char (chr): character to be searched
34
42
35
43
Returns :
36
44
i (int): index of char from last in pattern
37
45
-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
38
50
"""
39
51
40
52
for i in range (self .patLen - 1 , - 1 , - 1 ):
@@ -44,15 +56,19 @@ def match_in_pattern(self, char: str) -> int:
44
56
45
57
def mismatch_in_text (self , current_pos : int ) -> int :
46
58
"""
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.
49
61
50
62
Parameters :
51
63
current_pos (int): current index position of text
52
64
53
65
Returns :
54
66
i (int): index of mismatched char from last in text
55
67
-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
56
72
"""
57
73
58
74
for i in range (self .patLen - 1 , - 1 , - 1 ):
@@ -61,7 +77,14 @@ def mismatch_in_text(self, current_pos: int) -> int:
61
77
return - 1
62
78
63
79
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
+
65
88
positions = []
66
89
for i in range (self .textLen - self .patLen + 1 ):
67
90
mismatch_index = self .mismatch_in_text (i )
@@ -75,13 +98,7 @@ def bad_character_heuristic(self) -> list[int]:
75
98
return positions
76
99
77
100
78
- text = "ABAABA"
79
- pattern = "AB"
80
- bms = BoyerMooreSearch (text , pattern )
81
- positions = bms .bad_character_heuristic ()
101
+ if __name__ == "__main__" :
102
+ import doctest
82
103
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