-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_search.py
95 lines (77 loc) · 3.4 KB
/
naive_search.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def pattern_search(text: str, pattern: str, replace: str, case_sensitive: bool = True) -> str:
fixed_text=[]
skips = 0
# Prepare the text and pattern for case-insensitive comparison if needed
text_to_search = text if case_sensitive else text.lower()
pattern_to_search = pattern if case_sensitive else pattern.lower()
for i in range(len(text_to_search)):
match_count = 0
if skips > 0:
skips -= 1
continue
for j in range(len(pattern_to_search)):
if pattern_to_search[j] == text_to_search[i + j]:
match_count += 1
else:
break
if match_count == len(pattern):
skips = len(pattern) - 1
print(pattern, "found at index", i)
fixed_text.append(replace)
else:
fixed_text.append(text[i])
return "".join(fixed_text)
# Alternative method 1 using string slicing
def pattern_search_1(text: str, pattern: str, replace: str, case_sensitive: bool = True) -> str:
# If the pattern is empty, we can consider it as found
if pattern == "":
return text
fixed_text=[]
skips = 0
# Prepare the text and pattern for case-insensitive comparison if needed
text_to_search = text if case_sensitive else text.lower()
pattern_to_search = pattern if case_sensitive else pattern.lower()
# Check for the pattern at every possible starting position
for i in range(len(text_to_search)): # for i in range(len(text_to_search) - len(pattern_to_search) + 1), if not doing search and replace
if skips > 0:
skips -= 1
continue
if (i + len(pattern_to_search) <= len(text_to_search)) and (text_to_search[i:i + len(pattern_to_search)] == pattern_to_search):
skips = len(pattern) - 1
print(pattern_to_search, "found at index", i)
fixed_text.append(replace)
else:
fixed_text.append(text[i])
return "".join(fixed_text)
# Alternative method 2 using string slicing and a while loop instead of skips
def pattern_search_2(text: str, pattern: str, replace: str, case_sensitive: bool = True) -> str:
# If the pattern is empty, we can consider it as found
if pattern == "":
return text
fixed_text=[]
i = 0
# Prepare the text and pattern for case-insensitive comparison if needed
text_to_search = text if case_sensitive else text.lower()
pattern_to_search = pattern if case_sensitive else pattern.lower()
while i < len(text_to_search):
if (i + len(pattern_to_search) <= len(text_to_search)) and (text_to_search[i:i + len(pattern_to_search)] == pattern_to_search):
print(pattern_to_search, "found at index", i)
fixed_text.append(replace)
i += len(pattern_to_search)
else:
fixed_text.append(text[i])
i += 1
return "".join(fixed_text)
friends_intro = "Pylhon is a wonderful Language that zzz is beloved for its ease zzz of use and simple syntacs. While zzz at some times the performance can be less than iDil, by properly zzz utilizing built-in libraries and other languuUuage features, pylhon's performance zzz can approach that of C."
friends_intro = pattern_search_2(friends_intro, "Language", "language")
print(friends_intro)
friends_intro = pattern_search_2(friends_intro, "pylhon", "Python", False)
print(friends_intro)
friends_intro = pattern_search_2(friends_intro, "idil", "ideal", False)
print(friends_intro)
friends_intro = pattern_search_2(friends_intro, "zzz ", "")
print(friends_intro)
friends_intro = pattern_search_2(friends_intro, "syntacs", "syntax")
print(friends_intro)
friends_intro = pattern_search_2(friends_intro, "languuUuage", "language")
print(friends_intro)