Multisplit: Difference between revisions

14 bytes removed ,  13 years ago
Undo revision 106291 by Vincent (talk) This is still disputed in the talk page and should not be in the task description yet
(Returned back correct D version and put correct output in task condition.)
(Undo revision 106291 by Vincent (talk) This is still disputed in the talk page and should not be in the task description yet)
Line 5:
'''Extra Credit:''' include match information that indicates which separator was matched at each separation point and where in the input string that separator was matched.
 
Test your code using the input string “<code>a!===b=!=c!=d</code>” and the separators “<code>==</code>”, “<code>!=</code>” and “<code>=</code>”.
 
For these inputs the string should be parsed as <code>a != {==} =b {!=} c! {=} dc</code> (separators are enclosed in curlies).
 
=={{header|Ada}}==
Line 98:
 
=={{header|D}}==
<lang d>import std.stdio, std.array, std.algorithm;
<lang d>
import std.stdio, std.array, std.algorithm;
string[] multiSplit(in string s, string[] divisors) {
if (s.empty)
return [];
string[] result;
auto rest = s.idup;
foreachwhile (div; divisorstrue) {
auto p best= findSplit(rest, div)"";
auto result.length +delim= 1"";
bool result[$ - 1] done= p[0].iduptrue;
foreach (div; divisors) {
rest = p[2];
auto maybe= find(rest, div);
if (p[1].empty || rest.empty) // divisor is not found OR it was last in string
if (maybe.length > best.length) {
break;
best= maybe;
delim= div;
done= false;
}
}
result.length += 1;
if (s.emptydone) {
result[$ - 1] = rest.idup;
return []result;
} else {
auto t= findSplit(rest, delim);
result[$ - 1]= t[0].idup;
rest = pt[2];
}
}
if (!rest.empty) {
result.length += 1;
result[$ - 1] = rest.idup;
}
return result;
}
void main() {
auto s = "a!===b=!=c!=d";
auto divs = ["==", "!=", "="];
auto lst = multiSplit(s, divs);
Line 131 ⟶ 135:
foreach (i, p; lst) {
write(p);
if (i < lst.length-1) {
write(" {", divs[i], "} ");
}
}
writeln();
}</lang d>
}
Output (separator locations indicated by braces):
</lang>
<pre>a! {==} = {} b {!=} c! {=} dc</pre>
Output (separators are enclosed in curlies):
<pre>a! {==} =b {!=} c! {=} d</pre>
 
=={{header|F_Sharp|F#}}==
Anonymous user