Boyer-Moore string search: Difference between revisions

→‎{{header|Wren}}: Now includes Julia examples.
(→‎{{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 PythonJulia entry above.
<lang ecmascript>class BoyerMoore {
/**
Line 687:
}
 
var pat1texts = "put"[
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",
var pat2 = "and"
"needle need noodle needle",
var pat3 = "alfalfa"
text1 = "InhisbookseriesTheArtofComputerProgrammingpublishedbyAddisonWesleyDKnuthusesanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguagestoillustratetheconceptsandalgorithmsastheyarepresented",
System.print("text1 = %(text1)")
var text2 = "Nearby farms grew a half acre of alfalfa on the dairy's behalf, with bales of all that alfalfa exchanged for milk."
System.print("text2 = %(text2)")
]
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) {
System.print("Found '%(pat1)' in 'text1' at indices %(indicesOf.call(text1, pat1))")
var j = (i < 5) ? i : i-1
System.print("Found '%(pat2)' in 'text1' at indices %(indicesOf.call(text1, pat2))")
System.print("Found '%(pat3pats[i])' in 'text2text%(j+1)' at indices %(indicesOf.call(text2texts[j], pat3pats[i]))")</lang>
}</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
var text1text5 = "InhisbookseriesTheArtofComputerProgrammingpublishedbyAddisonWesleyDKnuthusesanimaginarycomputertheMIXanditsassociatedmachinecodeandassemblylanguagestoillustratetheconceptsandalgorithmsastheyarepresented"
text2text6 = Nearby farms grew a half acre of alfalfa on the dairy's behalf, with bales of all that alfalfa exchanged for milk.
 
Found 'putTCTA' in 'text1' at indices [266, 9014]
Found 'andTAATAAA' in 'text1text2' at indices [101, 128, 171]
Found 'alfalfaword' in 'text2text3' at indices [33, 8740]
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>
9,482

edits