Balanced brackets: Difference between revisions

Content added Content deleted
(→‎{{header|Batch File}}: Improved code and new example)
(Added Wren)
Line 6,988: Line 6,988:
: OK
: OK
[] : OK
[] : OK
</pre>

=={{header|Wren}}==
{{trans|Kotlin}}
<lang ecmascript>import "random" for Random

var isBalanced = Fn.new { |s|
if (s.isEmpty) return true
var countLeft = 0 // number of left brackets so far unmatched
for (c in s) {
if (c == "[") {
countLeft = countLeft + 1
} else if (countLeft > 0) {
countLeft = countLeft - 1
} else {
return false
}
}
return countLeft == 0
}

System.print("Checking examples in task description:")
var brackets = ["", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"]
for (b in brackets) {
System.write((b != "") ? b : "(empty)")
System.print("\t %(isBalanced.call(b) ? "OK" : "NOT OK")")
}
System.print("\nChecking 7 random strings of brackets of length 8:")
var rand = Random.new()
for (i in 1..7) {
var s = ""
for (j in 1..8) s = s + ((rand.int(2) == 0) ? "[" : "]")
System.print("%(s) %(isBalanced.call(s) ? "OK" : "NOT OK")")
}</lang>

{{out}}
<pre>
Checking examples in task description:
(empty) OK
[] OK
][ NOT OK
[][] OK
][][ NOT OK
[[][]] OK
[]][[] NOT OK

Checking 7 random strings of brackets of length 8:
[][][][[ NOT OK
[[][][]] OK
[[[[]]]] OK
[[]]]][[ NOT OK
]][][[]] NOT OK
][]][[[] NOT OK
[][][[][ NOT OK
</pre>
</pre>