Balanced brackets: Difference between revisions

Content added Content deleted
m (moved Balanced strings to Balanced brackets: More descriptive name, as discussed on Talk page)
m (whitespace)
Line 3: Line 3:
* Generate a string with <code>$N</code> opening brackets (<code>"["</code>) and <code>$N</code> closing brackets (<code>"]"</code>), in some arbitrary order.
* Generate a string with <code>$N</code> opening brackets (<code>"["</code>) and <code>$N</code> closing brackets (<code>"]"</code>), in some arbitrary order.
* Determine whether the string is <em>balanced</em>; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
* Determine whether the string is <em>balanced</em>; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
<br>


'''Examples''':
'''Examples''':
Line 42: Line 41:
"[[]][[[[]]]]" is balanced
"[[]][[[[]]]]" is balanced
"[[]][][]]]][[" is not balanced</pre>
"[[]][][]]]][[" is not balanced</pre>



=={{header|J}}==
=={{header|J}}==
Line 149: Line 147:


=={{header|Python}}==
=={{header|Python}}==

<lang python>>>> def gen(N):
<lang python>>>> def gen(N):
... txt = ['['] * N + [']'] * N
... txt = ['['] * N + [']'] * N
Line 225: Line 222:
"]][[][[][[[[]][[][]][[]]]]][" -> NOT OK
"]][[][[][[[[]][[][]][[]]]]][" -> NOT OK
</pre>
</pre>

=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<lang tuscript>$$ MODE DATA
$$ MODE DATA
$$ SET brackets=*
$$ SET brackets=*
[
[
Line 256: Line 253:
status=CHECK_BRACKETS (b,brackets,a1,e1,a2,e2)
status=CHECK_BRACKETS (b,brackets,a1,e1,a2,e2)
PRINT b," ",status
PRINT b," ",status
ENDLOOP
ENDLOOP</lang>
</lang>
Output:
Output:
<pre>
<pre style='height:30ex;overflow:scroll'>
[ ERROR
[ ERROR
[] OK
[] OK