Word break problem: Difference between revisions
Content added Content deleted
(→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.) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: 'strict' compatible) |
||
Line 501: | Line 501: | ||
=={{header|Perl}}== |
=={{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. |
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> |
<lang perl>use 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>; |
print "$_: " . word_break($_,@words) . "\n" for <a amiss parable opera operable inoperable permission mississippi>; |
||
Line 510: | Line 513: | ||
my $one_of = join '|', @dictionary; |
my $one_of = join '|', @dictionary; |
||
@matches = $word =~ /^ ($one_of)(?!\1) (?:($one_of)(?!\2))? (?:($one_of)(?!\3))? (?:($one_of)(?!\4))? $/x; |
@matches = $word =~ /^ ($one_of)(?!\1) (?:($one_of)(?!\2))? (?:($one_of)(?!\3))? (?:($one_of)(?!\4))? $/x; |
||
return join(' ', @matches) || "(not possible)"; |
return join(' ', grep {$_} @matches) || "(not possible)"; |
||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |