Jump to content

Word break problem: Difference between revisions

Added Tailspin solution
m (added related tasks.)
(Added Tailspin solution)
Line 1,731:
abcdd:
did not parse with given words</pre>
 
=={{header|Tailspin}}==
Does a depth-first search (we could easily stop looking after one is found, just add a condition to do nothing if a done-flag is set)
<lang tailspin>
templates wordBreaks&{dict:}
composer starts&{with:}
<does|not>
rule does: (<=$with>) <'.*'>
rule not: <'.*'> -> \(!VOID\)
end starts
'Breaking "$;" by $dict;:$#10;' -> !OUT::write
{ words:[], remaining: $} -> #
'--done--$#10;' !
 
when <{remaining: <=''>}> do
$.words -> \spaceSeparate[i](when <?($i <=1>)> do $! otherwise ' ' ! $ ! \spaceSeparate) -> '$...;$#10;' !
otherwise
def base: $;
$dict... -> \(
def word: $;
$base.remaining -> starts&{with: $word} -> {words: [$base.words..., $word], remaining: $} !
\) -> #
end wordBreaks
 
'ababab' -> wordBreaks&{dict: ['a', 'ab', 'bab']} -> !OUT::write
'abcbab' -> wordBreaks&{dict: ['a', 'ab', 'bab']} -> !OUT::write
</lang>
{{out}}
<pre>
Breaking "ababab" by [a, ab, bab]:
a bab ab
ab a bab
ab ab ab
--done--
Breaking "abcbab" by [a, ab, bab]:
--done--
</pre>
 
=={{header|Visual Basic .NET}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.