Balanced brackets: Difference between revisions

no edit summary
m (→‎{{header|RPL}}: reformatting)
No edit summary
Line 4,893:
print(isBalanced(RS))
</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
module Balanced_brackets{
// Generate a string with N opening brackets [ and with N closing brackets ], in some arbitrary order.
function randomBrackets(max as long) {
if max<2 then max=1
def putbracket()=mid$("[[[[]]",random(1,6),1)
dim a(random(1, max*2)) as string<<putbracket()
=a()#str$("")
}
// Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
function check(s as string) {
long i, level
for i=1 to len(s)
if mid$(s,i,1)="[" then level++ else level--
if level<0 then exit
next
=level=0
}
string k
boolean m
do
k=randomBrackets(5)
m=check(k)
print k;@(12);": ";if$(m->"OK", "NOT OK")
until m
}
Balanced_brackets
</syntaxhighlight>
{{out}}
<pre>
[[[] : NOT OK
[[][]] : OK
</pre>
 
=={{header|Maple}}==
404

edits