-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Naive pattern search algorithm (#175)
* add naive pattern search algorithm * add naive pattern search algorithm Co-authored-by: Laptop-Salad <email.com>
- Loading branch information
1 parent
96a0c2e
commit 49e93a1
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
naive_pattern_search(text, pattern) | ||
Program to find the given pattern in the given text | ||
# Arguments: | ||
- 'text': A string to find the pattern | ||
- 'pattern': A string to find in the given text | ||
# Examples/Tests | ||
```julia | ||
julia> naive_pattern_search("ABCDEF", "DEF") | ||
"DEF found at index: 3" | ||
julia> naive_pattern_search("Hello world!", "eggs") | ||
"No matches found" | ||
``` | ||
# References: | ||
(https://www.geeksforgeeks.org/naive-algorithm-for-pattern-searching/) | ||
(https://www.tutorialspoint.com/Naive-Pattern-Searching) | ||
# Contributors: | ||
- [Laptop-Salad](https://github.com/Laptop-Salad) | ||
""" | ||
|
||
function naive_pattern_search(text, pattern) | ||
for index in 0:(length(text)-length(pattern) + 1) | ||
matches = 0 | ||
for character in eachindex(pattern) | ||
if pattern[character] == text[index + character] | ||
matches += 1 | ||
|
||
if matches == length(pattern) | ||
return string(pattern, " found at index: ", index) | ||
end | ||
else | ||
break | ||
end | ||
end | ||
end | ||
return "No matches found" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters