Balanced brackets: Difference between revisions

Added MiniScript version
(Added solution for Action!)
(Added MiniScript version)
Line 4,699:
).
</lang>
 
=={{header|MiniScript}}==
 
We start by defining a function:
 
<lang MiniScript>isBalanced = function(str)
level = 0
for c in str
if c == "[" then level = level + 1
if c == "]" then level = level - 1
if level < 0 then return false
end for
return true
end function</lang>
 
We can evaluate the example strings with this code:
 
<lang MiniScript>examples = [
"",
"[]",
"[][]",
"[[][]]",
"][",
"][][",
"[]][[]"]
 
for str in examples
balanced = isBalanced(str)
if balanced then outcome = "is OK" else outcome = "NOT OK"
print """" + str + """ " + outcome
end for</lang>
 
{{out}}
<pre>"" is OK
"[]" is OK
"[][]" is OK
"[[][]]" is OK
"][" NOT OK
"][][" NOT OK
"[]][[]" NOT OK</pre>
 
=={{header|Modula-2}}==
Anonymous user