Repository navigation
Expand file tree
/
Copy pathbryce_algorithm.py
More file actions
43 lines (39 loc) · 1.74 KB
/
Copy pathbryce_algorithm.py
File metadata and controls
43 lines (39 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#This is where I will code the algorithm that I am creating
def boyer_moore_algorithm(text, pattern):
"""
A function that takes in a text string and searches for a given pattern within the text.
Args:
text: A string of all the text you want the algorithm to search through.
pattern: A string of the pattern that you want to search for in the given string of text.
Returns:
The number of times that the pattern was found in the given text.
"""
m = len(pattern)
n = len(text)
#If the pattern is longer than the text than there can't be any matches
if m > n:
return 0
else:
#Limit the alphabet because we know that there are no other characters in the text we are using
#Initialize to the length of the pattern so that if none of these characters are found in the pattern then we skip a whole sequence
skip = {'a': m, 'b': m, 'c': m, 'd': m, 'e': m}
#Find last(c) for each character in the pattern
for k in range(m-1):
skip[pattern[k]] = m - k - 1
#Initalize the spot in the text we are looking at (the first possible ending of a pattern in the text)
i = m - 1
count = 0
#Loop through the entire text
while i < n:
j = m - 1
k = i
#Loop through the entire pattern to see if we have found a match in the text
while j >= 0 and text[k] == pattern[j]:
j -= 1
k -= 1
if j == -1:
#We have found a pattern match!
count += 1
#Skip ahead in the text by that last(c) value or the length of the pattern if the character isn't in the dict
i += skip.get(text[i], m)
return count