Multisplit: Difference between revisions

Returned back correct D version and put correct output in task condition.
(add Ada)
(Returned back correct D version and put correct output in task condition.)
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! {=} cd</code> (separators are enclosed in curlies).
 
=={{header|Ada}}==
Line 98:
 
=={{header|D}}==
}</lang d>
<lang d>import std.stdio, std.array, std.algorithm;
string[] multiSplit(in string s, string[] divisors) {
if (dones.empty) {
return result[];
string[] result;
auto rest = s.idup;
whileforeach (truediv; divisors) {
auto bestp = ""findSplit(rest, div);
auto delim result.length += ""1;
bool done result[$ - 1] = truep[0].idup;
rest = tp[2];
foreach (div; divisors) {
if (p[1].empty || rest.empty) // divisor is not found OR it was last in string
auto maybe= find(rest, div);
break;
if (maybe.length > best.length) {
best= maybe;
delim= div;
done= false;
}
}
result.length+= 1;
if (done) {
result[$ - 1]= rest.idup;
return result;
} else {
auto t= findSplit(rest, delim);
result[$ - 1]= t[0].idup;
rest= t[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 135 ⟶ 131:
foreach (i, p; lst) {
write(p);
if (i < lst.length-1) {
write(" {", divs[i], "} ");
}
}
writeln();
}
}</lang>
</lang>
Output (separator locations indicated by braces):
Output (separators are enclosed in curlies):
<pre>a! {==} {} =b {!=} c! {=} cd</pre>
 
=={{header|F_Sharp|F#}}==
19

edits