How to resolve the algorithm Count occurrences of a substring step by step in the Julia programming language
How to resolve the algorithm Count occurrences of a substring step by step in the Julia programming language
Table of Contents
Problem Statement
Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments:
It should return an integer count. The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left (see proof on talk page).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Count occurrences of a substring step by step in the Julia programming language
The provided Julia code takes two lists, ts
and tsub
, and demonstrates the use of the matchall
function to count the occurrences of substrings in strings. The code performs both non-overlapping and overlapping substring counts using regular expressions. Here's a step-by-step explanation:
-
Initialization:
ts
andtsub
are initialized with two sets of strings.ts
contains longer strings, andtsub
contains substrings to search for.
-
Non-Overlapping Substring Count:
- The code iterates over the elements of
ts
using afor
loop. - For each
ts[i]
, it prints the string and its corresponding substringtsub[i]
. - It then calls
matchall(Regex(tsub[i]), ts[i])
to find all non-overlapping occurrences oftsub[i]
ints[i]
. - The length of the resulting array is printed, which gives the count of non-overlapping occurrences.
- The code iterates over the elements of
-
Overlapping Substring Count:
- After completing the non-overlapping count, the code performs a similar process for overlapping substring occurrences.
- It iterates over
ts
again and prints the strings and substrings. - This time, it calls
matchall(Regex(tsub[i]), ts[i], true)
with an additionaltrue
argument. This argument enables overlapping matches, allowing substrings to overlap in the counting process. - It prints the length of the resulting array, which gives the count of overlapping occurrences.
-
Output:
- The code prints the results of both non-overlapping and overlapping substring counts in the following format:
"String (SubString) => Count"
- The code prints the results of both non-overlapping and overlapping substring counts in the following format:
For example, if ts=["the three truths", "ababababab"]
and tsub=["th", "abab"]
, the output might look like this:
Test of non-overlapping substring counts.
the three truths (th) => 1
ababababab (abab) => 2
Test of overlapping substring counts.
the three truths (th) => 3
ababababab (abab) => 4
Source code in the julia programming language
ts = ["the three truths", "ababababab"]
tsub = ["th", "abab"]
println("Test of non-overlapping substring counts.")
for i in 1:length(ts)
print(ts[i], " (", tsub[i], ") => ")
println(length(matchall(Regex(tsub[i]), ts[i])))
end
println()
println("Test of overlapping substring counts.")
for i in 1:length(ts)
print(ts[i], " (", tsub[i], ") => ")
println(length(matchall(Regex(tsub[i]), ts[i], true)))
end
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Wren programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Program termination step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Commodore BASIC programming language