Balanced brackets: Difference between revisions

Content added Content deleted
(→‎{{header|Tcl}}: Marked incomplete as strings are not generated)
Line 11: Line 11:
=={{header|D}}==
=={{header|D}}==
{{incomplete|D|Strings are not generated.}}
{{incomplete|D|Strings are not generated.}}
<lang d>import std.stdio, std.algorithm, std.string;
<lang d>import std.stdio, std.algorithm, std.string, std.range, std.random, std.conv;

auto generate(int n) {
return text(map!((i){ return "[]"[uniform(0,2)]; })(iota(n)));
}


void main() {
void main() {
foreach (s; ",[],[][],[[][]],][,][][,[]][[]".split(","))
foreach (i; 0 .. 14) {
auto s = generate(i);
writefln("%-8s is%s balanced", '"' ~ s ~ '"',
s.balancedParens('[', ']') ? "" : " not");
writefln("%-15s is%s balanced", '"' ~ s ~ '"',
s.balancedParens('[', ']') ? "" : " not");
}
}</lang>
}</lang>
Output:
Output:
<pre>"" is balanced
<pre>"" is balanced
"[]" is balanced
"[" is not balanced
"[][]" is balanced
"][" is not balanced
"[[][]]" is balanced
"[]]" is not balanced
"][" is not balanced
"][][" is not balanced
"][][" is not balanced
"][][]" is not balanced
"[]][[]" is not balanced</pre>
"[[[]][" is not balanced
"[]][[[]" is not balanced
"][][][[]" is not balanced
"][]][][[]" is not balanced
"[]][[]][[]" is not balanced
"][[]]][]]][" is not balanced
"[[]][[[[]]]]" is balanced
"[[]][][]]]][[" is not balanced</pre>


=={{header|Perl 6}}==
=={{header|Perl 6}}==