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