Boyer-Moore string search: Difference between revisions
→{{header|Wren}}: Now includes Julia examples.
m (→{{header|Julia}}: typo) |
(→{{header|Wren}}: Now includes Julia examples.) |
||
Line 573:
I've worked here from the Java code in the Wikipedia article though, as this version only finds the index of the first occurrence of the substring, I've added a routine to find all non-overlapping occurrences by repeatedly calling the ''BoyerMoore.indexOf'' method. Indices are zero-based.
The same examples have been used as in the
<lang ecmascript>class BoyerMoore {
/**
Line 687:
}
var text1 = "InhisbookseriesTheArtofComputerProgrammingpublishedbyAddisonWesleyDKnuthusesanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguagestoillustratetheconceptsandalgorithmsastheyarepresented"▼
"GCTAGCTCTACGAGTCTA",
var text2 = "Nearby farms grew a half acre of alfalfa on the dairy's behalf, with bales of all that alfalfa exchanged for milk."▼
"GGCTATAATGCGTA",
▲var pat1 = "put"
"there would have been a time for such a word",
"needle need noodle needle",
▲
]
var pats = ["TCTA", "TAATAAA", "word", "needle", "put", "and", "alfalfa"]
for (i in 0...texts.count) System.print("text%(i+1) = %(texts[i])")
System.print()
for (i in 0...pats.count) {
var j = (i < 5) ? i : i-1
System.print("Found '%(
}</lang>
{{out}}
<pre>
text1 = GCTAGCTCTACGAGTCTA
▲text1 = InhisbookseriesTheArtofComputerProgrammingpublishedbyAddisonWesleyDKnuthusesanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguagestoillustratetheconceptsandalgorithmsastheyarepresented
text2 = GGCTATAATGCGTA
text2 = Nearby farms grew a half acre of alfalfa on the dairy's behalf, with bales of all that alfalfa exchanged for milk.▼
text3 = there would have been a time for such a word
text4 = needle need noodle needle
▲
▲
Found '
Found '
Found '
Found 'needle' in 'text4' at indices [0, 19]
Found 'put' in 'text5' at indices [26, 90]
Found 'and' in 'text5' at indices [101, 128, 171]
Found 'alfalfa' in 'text6' at indices [33, 87]
</pre>
|