Jump to content

Word break problem: Difference between revisions

m
→‎{{header|Perl}}: 'strict' compatible
(→‎Functional Python: Removed latest tendentious intervention. Pythonic Python is a good dialect of the language, but not the language itself, and is deliberately not optimised for functional programming. Time for you to abandon this silly campaign.)
m (→‎{{header|Perl}}: 'strict' compatible)
Line 501:
=={{header|Perl}}==
Look for words in alternation <code>$one_of</code>, but don't allow repeats (<code>?!\1</code> for the 1st, <code>?!\2</code> for the 2nd, etc). Easily extended by adding more terms to the pattern.
<lang perl>myuse @words = <a o is pi ion par per sip miss able>strict;
use warnings;
 
my @words = <a o is pi ion par per sip miss able>;
 
print "$_: " . word_break($_,@words) . "\n" for <a amiss parable opera operable inoperable permission mississippi>;
Line 510 ⟶ 513:
my $one_of = join '|', @dictionary;
@matches = $word =~ /^ ($one_of)(?!\1) (?:($one_of)(?!\2))? (?:($one_of)(?!\3))? (?:($one_of)(?!\4))? $/x;
return join(' ', grep {$_} @matches) || "(not possible)";
}</lang>
{{out}}
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.