Balanced brackets: Difference between revisions

Added uBasic/4tH version
(→‎{{header|Perl}}: make the code do what was actually asked; make regex solution more idiomatic and put it first; remove use of deprecated 'when' keyword)
imported>Thebeez
(Added uBasic/4tH version)
 
(289 intermediate revisions by more than 100 users not shown)
Line 1:
{{task}}
 
'''Task''':
* Generate a string with <math>\mathrm{&nbsp; '''N}</math>''' &nbsp; opening brackets (“&nbsp; <codebig>'''['''</codebig>”) &nbsp; and <math>\mathrm{with &nbsp; '''N}</math>''' &nbsp; closing brackets (“&nbsp; <codebig>''']'''</codebig>”), &nbsp; in some arbitrary order.
* 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.
 
'''Examples''':
 
 
(empty) OK
;Examples:
[] OK ][ NOT OK
[][](empty) OK ][][ NOT OK
[[][]] OK []][[] NOTOK OK
[][] OK
[[][]] OK
][ NOT OK
][][ NOT OK
[]][[] NOT OK
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F gen(n)
V txt = [‘[’, ‘]’] * n
random:shuffle(&txt)
R txt.join(‘’)
 
F is_balanced(s)
V nesting_level = 0
L(c) s
S c
‘[’
nesting_level++
‘]’
I --nesting_level < 0
R 0B
R 1B
 
L(n) 0..9
V s = gen(n)
print(s‘’(‘ ’ * (20 - s.len))‘is ’(I is_balanced(s) {‘balanced’} E ‘not balanced’))</syntaxhighlight>
{{out}}
<pre>
is balanced
[] 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
</pre>
 
=={{header|360 Assembly}}==
<syntaxhighlight lang="360asm">* Balanced brackets 28/04/2016
BALANCE CSECT
USING BALANCE,R13 base register and savearea pointer
SAVEAREA B STM-SAVEAREA(R15)
DC 17F'0'
STM STM R14,R12,12(R13)
ST R13,4(R15)
ST R15,8(R13)
LR R13,R15 establish addressability
LA R8,1 i=1
LOOPI C R8,=F'20' do i=1 to 20
BH ELOOPI
MVC C(20),=CL20' ' c=' '
LA R1,1
LA R2,10
BAL R14,RANDOMX
LR R11,R0 l=randomx(1,10)
SLA R11,1 l=l*2
LA R10,1 j=1
LOOPJ CR R10,R11 do j=1 to 2*l
BH ELOOPJ
LA R1,0
LA R2,1
BAL R14,RANDOMX
LR R12,R0 m=randomx(0,1)
LTR R12,R12 if m=0
BNZ ELSEM
MVI Q,C'[' q='['
B EIFM
ELSEM MVI Q,C']' q=']'
EIFM LA R14,C-1(R10) @c(j)
MVC 0(1,R14),Q c(j)=q
LA R10,1(R10) j=j+1
B LOOPJ
ELOOPJ BAL R14,CHECKBAL
LR R2,R0
C R2,=F'1' if checkbal=1
BNE ELSEC
MVC PG+24(2),=C'ok' rep='ok'
B EIFC
ELSEC MVC PG+24(2),=C'? ' rep='? '
EIFC XDECO R8,XDEC i
MVC PG+0(2),XDEC+10
MVC PG+3(20),C
XPRNT PG,26
LA R8,1(R8) i=i+1
B LOOPI
ELOOPI L R13,4(0,R13)
LM R14,R12,12(R13)
XR R15,R15 set return code to 0
BR R14 -------------- end
CHECKBAL CNOP 0,4 -------------- checkbal
SR R6,R6 n=0
LA R7,1 k=1
LOOPK C R7,=F'20' do k=1 to 20
BH ELOOPK
LR R1,R7 k
LA R4,C-1(R1) @c(k)
MVC CI(1),0(R4) ci=c(k)
CLI CI,C'[' if ci='['
BNE NOT1
LA R6,1(R6) n=n+1
NOT1 CLI CI,C']' if ci=']'
BNE NOT2
BCTR R6,0 n=n-1
NOT2 LTR R6,R6 if n<0
BNM NSUP0
SR R0,R0 return(0)
B RETCHECK
NSUP0 LA R7,1(R7) k=k+1
B LOOPK
ELOOPK LTR R6,R6 if n=0
BNZ ELSEN
LA R0,1 return(1)
B RETCHECK
ELSEN SR R0,R0 return(0)
RETCHECK BR R14 -------------- end checkbal
RANDOMX CNOP 0,4 -------------- randomx
LR R3,R2 i2
SR R3,R1 ii=i2-i1
L R5,SEED
M R4,=F'1103515245'
A R5,=F'12345'
SRDL R4,1 shift to improve the algorithm
ST R5,SEED seed=(seed*1103515245+12345)>>1
LR R6,R3 ii
LA R6,1(R6) ii+1
L R5,SEED seed
LA R4,0 clear
DR R4,R6 seed//(ii+1)
AR R4,R1 +i1
LR R0,R4 return(seed//(ii+1)+i1)
BR R14 -------------- end randomx
SEED DC F'903313037'
C DS 20CL1
Q DS CL1
CI DS CL1
PG DC CL80' '
XDEC DS CL12
REGS
END BALANCE</syntaxhighlight>
{{out}}
<pre>
1 ][[[][[] ?
2 ]][]][][[[[][[ ?
3 [] ok
4 ][ ?
5 ][[][]]]]] ?
6 ][]] ?
7 ]][][][]]] ?
8 [[[[][[[[][[]] ?
9 ][[]][[[[[[[[[]]][ ?
10 ]]]][][[][]]][][[[ ?
11 [][[]][][][[[] ?
12 ]] ?
13 [][[[]]] ok
14 ][ ?
15 []][ ?
16 ][[]][]]][[] ?
17 ][][]] ?
18 [] ok
19 [[[[[[][[[[[][][ ?
20 [[][[][] ?
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program balencebrac64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "Expression : "
szMessBalenced: .asciz " balanced"
szMessNotBalenced: .asciz " not balanced"
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24 // conversion buffer
sBuffer: .skip 80
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
mov x0,0b111000 // bit 1 = open bracket bit 0 = close bracket
bl testBalanced
 
mov x0,0b110100
bl testBalanced
mov x0,0b11001001
bl testBalanced
mov x19,10 // number random test
1:
mov x0,1 // mini number
mov x1,10000 // maxi random number
bl extRandom // generate random number
bl testBalanced
subs x19,x19,1
bge 1b
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessResult: .quad szMessResult
qAdrszMessNotBalenced: .quad szMessNotBalenced
qAdrszMessBalenced: .quad szMessBalenced
qAdrszMessStart: .quad szMessStart
qAdrsBuffer: .quad sBuffer
/***************************************************/
/* routine to test expression */
/***************************************************/
/* x0 expression */
testBalanced:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
ldr x1,qAdrsBuffer
bl isBalanced
cmp x0,0
ldr x3,qAdrszMessNotBalenced
ldr x4,qAdrszMessBalenced
csel x3,x3,x4,eq
 
mov x0,#4 // string number to display
ldr x1,qAdrszMessResult
ldr x2,qAdrsBuffer
ldr x4,qAdrszCarriageReturn
bl displayStrings // display message
100:
ldp x2,x3,[sp],16 // restaur registres
ldp x1,lr,[sp],16 // restaur registres
ret
/***************************************************/
/* control if expression is balenced */
/***************************************************/
/* x0 expression */
/* x1 buffer address */
/* x0 return 1 if balanced else zero */
isBalanced:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
mov x3,63
clz x2,x0 // number of zeros on the left
sub x2,x3,x2 // so many useful numbers right
mov x4,1 // mask to test bit
lsl x4,x4,x2 // shift left begin expression
mov x3,0 // top if right bracket > left bracket
mov x7,0 // indice display buffer expression
mov x5,0 // counter brackets
1: // begin loop to test bits
tst x0,x4
beq 2f // bit = 0
mov x6,'(' // else bit = 1 -> open bracket
strb w6,[x1,x7] // store in buffer
add x7,x7,1 // increment indice
add x5,x5,1 // increment open bracket
b 3f
2: // bit = 0
mov x6,')' // close bracket
strb w6,[x1,x7] // store in buffer
add x7,x7,1 // increment indice
subs x5,x5,1 // decrement open bracket
bge 3f // if negative
mov x3,1 // top error
3:
lsr x4,x4,1 // shift mask right
cbnz x4,1b // and loop if not zero
strb wzr,[x1,x7] // zero final on buffer
cmp x5,0 // right bracket <> left bracket -> error
bne 4f
cmp x3,0 // in expression left bracket > right bracket
bne 4f
mov x0,1 // balanced
b 100f
4:
mov x0,0 // not balanced
100:
ldp x2,x3,[sp],16 // restaur registres
ldp x1,lr,[sp],16 // restaur registres
ret
/***************************************************/
/* display multi strings */
/* new version 24/05/2023 */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* x4 address string4 */
/* x5 address string5 */
/* x6 address string5 */
/* x7 address string6 */
displayStrings: // INFO: displayStrings
stp x8,lr,[sp,-16]! // save registers
stp x2,fp,[sp,-16]! // save registers
add fp,sp,#32 // save paraméters address (4 registers saved * 8 bytes)
mov x8,x0 // save strings number
cmp x8,#0 // 0 string -> end
ble 100f
mov x0,x1 // string 1
bl affichageMess
cmp x8,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x8,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x8,#3
ble 100f
mov x0,x4
bl affichageMess
cmp x8,#4
ble 100f
mov x0,x5
bl affichageMess
cmp x8,#5
ble 100f
mov x0,x6
bl affichageMess
cmp x8,#6
ble 100f
mov x0,x7
bl affichageMess
100:
ldp x2,fp,[sp],16 // restaur registers
ldp x8,lr,[sp],16 // restaur registers
ret
/******************************************************************/
/* random number */
/******************************************************************/
/* x0 contains inferior value */
/* x1 contains maxi value */
/* x0 return random number */
extRandom:
stp x1,lr,[sp,-16]! // save registers
stp x2,x8,[sp,-16]! // save registers
stp x19,x20,[sp,-16]! // save registers
sub sp,sp,16 // reserve 16 octets on stack
mov x19,x0
add x20,x1,1
mov x0,sp // store result on stack
mov x1,8 // length 8 bytes
mov x2,0
mov x8,278 // call system Linux 64 bits Urandom
svc 0
mov x0,sp // load résult on stack
ldr x0,[x0]
sub x2,x20,x19 // calculation of the range of values
udiv x1,x0,x2 // calculation range modulo
msub x0,x1,x2,x0
add x0,x0,x19 // and add inferior value
100:
add sp,sp,16 // alignement stack
ldp x19,x20,[sp],16 // restaur 2 registers
ldp x2,x8,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // retour adresse lr x30
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Expression : ((())) balanced
Expression : (()()) balanced
Expression : (())())( not balanced
Expression : ((()))))()))) not balanced
Expression : (()())())())) not balanced
Expression : ()(((()((()) not balanced
Expression : ())()(()())() not balanced
Expression : ())))((()))((( not balanced
Expression : (())()())(() not balanced
Expression : ())()))))(()( not balanced
Expression : ())()(())(()( not balanced
Expression : ())(()(()())) not balanced
Expression : ((()))()))))) not balanced
Expression : (()))((()))( not balanced
</pre>
 
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
CLASS lcl_balanced_brackets DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
class_constructor,
 
are_brackets_balanced
IMPORTING
seq TYPE string
RETURNING
VALUE(r_are_brackets_balanced) TYPE abap_bool,
 
get_random_brackets_seq
IMPORTING
n TYPE i
RETURNING
VALUE(r_bracket_seq) TYPE string.
 
PRIVATE SECTION.
CLASS-DATA: random_int TYPE REF TO cl_abap_random_int.
 
CLASS-METHODS:
_split_string
IMPORTING
i_text TYPE string
RETURNING
VALUE(r_chars) TYPE stringtab,
 
_rand_bool
RETURNING
VALUE(r_bool) TYPE i.
ENDCLASS.
 
CLASS lcl_balanced_brackets IMPLEMENTATION.
METHOD class_constructor.
random_int = cl_abap_random_int=>create( seed = CONV #( sy-uzeit )
min = 0
max = 1 ).
ENDMETHOD.
 
METHOD are_brackets_balanced.
DATA: open_bracket_count TYPE i.
 
DATA(chars) = _split_string( seq ).
 
r_are_brackets_balanced = abap_false.
 
LOOP AT chars ASSIGNING FIELD-SYMBOL(<c>).
IF <c> = ']' AND open_bracket_count = 0.
RETURN.
ENDIF.
 
IF <c> = ']'.
open_bracket_count = open_bracket_count - 1.
ENDIF.
 
IF <c> = '['.
open_bracket_count = open_bracket_count + 1.
ENDIF.
ENDLOOP.
 
IF open_bracket_count > 0.
RETURN.
ENDIF.
 
r_are_brackets_balanced = abap_true.
ENDMETHOD.
 
METHOD get_random_brackets_seq.
DATA(itab) = VALUE stringtab( FOR i = 1 THEN i + 1 WHILE i <= n
( COND #( WHEN _rand_bool( ) = 0 THEN '['
ELSE ']' ) ) ).
r_bracket_seq = concat_lines_of( itab ).
ENDMETHOD.
 
METHOD _rand_bool.
r_bool = random_int->get_next( ).
ENDMETHOD.
 
METHOD _split_string.
DATA: off TYPE i VALUE 0.
 
DO strlen( i_text ) TIMES.
INSERT i_text+off(1) INTO TABLE r_chars.
off = off + 1.
ENDDO.
ENDMETHOD.
ENDCLASS.
 
START-OF-SELECTION.
DO 10 TIMES.
DATA(seq) = lcl_balanced_brackets=>get_random_brackets_seq( 10 ).
cl_demo_output=>write( |{ seq } => { COND string( WHEN lcl_balanced_brackets=>are_brackets_balanced( seq ) = abap_true THEN 'OK'
ELSE 'NOT OK' ) }| ).
ENDDO.
cl_demo_output=>display( ).
 
</syntaxhighlight>
 
{{out}}
<pre>
[[]][[]]]] => NOT OK
 
][]][[][][ => NOT OK
 
[][]]][[[] => NOT OK
 
][][[[][[] => NOT OK
 
[[[][]]][] => OK
 
][][]][[[[ => NOT OK
 
][][[[[]][ => NOT OK
 
][[][]][[] => NOT OK
 
[[]][[]][] => OK
 
[][]]][]]] => NOT OK
 
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Generate(BYTE size CHAR ARRAY s)
BYTE i,half
 
s(0)=size
half=size RSH 1
 
FOR i=1 TO half
DO s(i)='[ OD
 
FOR i=half+1 TO size
DO s(i)='] OD
RETURN
 
PROC Shuffle(CHAR ARRAY s)
BYTE i,j,k,n,len,tmp
 
len=s(0)
n=Rand(len+1)
FOR k=1 TO n
DO
i=Rand(len)+1
j=Rand(len)+1
tmp=s(i)
s(i)=s(j)
s(j)=tmp
OD
RETURN
 
BYTE FUNC Balanced(CHAR ARRAY s)
INT i,lev
lev=0
FOR i=1 TO s(0)
DO
IF s(i)='[ THEN
lev==+1
ELSE
lev==-1
FI
 
IF lev<0 THEN
RETURN (0)
FI
OD
 
IF lev#0 THEN
RETURN (0)
FI
RETURN (1)
 
PROC Main()
CHAR ARRAY s(20)
BYTE i,b
 
FOR i=0 TO 20 STEP 2
DO
Generate(i,s)
Shuffle(s)
b=Balanced(s)
 
IF s(0)=0 THEN
Print("(empty)")
ELSE
Print(s)
FI
Print(" is ")
IF b=0 THEN
Print("not ")
FI
PrintE("balanced")
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Balanced_brackets.png Screenshot from Atari 8-bit computer]
<pre>
(empty) is balanced
[] is balanced
[[]] is balanced
][][[] is not balanced
[]][[][] is not balanced
[[[]]]][[] is not balanced
[[[[[[]]]]]] is balanced
[[]][[[][]]][] is balanced
[[[[]][[]]]][]][ is not balanced
][][][[[[[]]][]]][ is not balanced
[[[[][][][][]][]]][] is balanced
</pre>
 
=={{header|Acurity Architect}}==
<pre>
Using #HASH-OFF
</pre>
<syntaxhighlight lang="acurity architect">
FUNCTION bBRACKETS_MATCH(zStringWithBrackets: STRING): STRING
VAR sCount: SHORT
VAR sBracketCounter: SHORT
VAR zOK: STRING
//
SET zOK = "NOT OK"
DO sCount = 1 TO LENGTH(zStringWithBrackets)
CASE SUBSTR(zStringWithBrackets, sCount, 1)
VALUE "["
SET sBracketCounter = sBracketCounter + 1
VALUE "]"
SET sBracketCounter = sBracketCounter - 1
ENDCASE
ENDDO
IF sBracketCounter = 0
SET zOK = "OK"
ENDIF
RETURN zOK
ENDFUNCTION
</syntaxhighlight>
{{out}}
<pre>
bBRACKETS_MATCH("[][][][][][][[[[[]]]]]") = "OK"
bBRACKETS_MATCH("sdapasd[a]dfa[fdf][f]a[era[d]as[a]sd[as][da]s[") = "NOT OK"
bBRACKETS_MATCH("3r acwf4[a][ a]sg5s]4t[5e4][taw][ra][r] c[ra]2[r]") = "NOT OK"
bBRACKETS_MATCH("234 rq4ctac3rc[q2 ]r[q4tq4 ]t[4v5 y7 [e6y[]a[45 rv[2q]5q[2q3") = "NOT OK"
</pre>
 
=={{header|Ada}}==
brackets.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
with Ada.Strings.Fixed;
Line 69 ⟶ 724:
end loop;
end loop;
end Brackets;</langsyntaxhighlight>
 
Output:
Line 90 ⟶ 745:
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">unbalanced(data s)
<lang aime>integer
unbalanced(text s)
{
integer b, i;
 
b = i = 0;
while (i =+ ~s && -1 < b) 0;{
b += s[i -= 1] == '[' ? -1 : 1;
while (i < length(s)) {
if (character(s, i) == '[') {
b += 1;
} else {
b -= 1;
if (b < 0) {
break;
}
}
 
i += 1;
}
 
return b;
}
 
generate(data b, integer d)
text
generate(integer d)
{
integerif i;(d) {
d.times(l_bill, list(), -1, '[', ']').l_rand().ucall(b_append, 1, b);
text s;
 
i = d;
while (i) {
s = insert(s, 0, '[');
i -= 1;
}
 
i = d;
while (i) {
s = insert(s, drand(length(s)), ']');
i -= 1;
}
 
return s;
}
 
integer
main(void)
{
Line 141 ⟶ 770:
i = 0;
while (i < 10) {
textdata s;
 
generate(s, = generate(i);
o_(s, " is ", unbalanced(s) ? "un" : "", "balanced\n");
o_text(s);
o_text(" is ");
if (unbalanced(s)) {
o_text("un");
}
o_text("balanced\n");
 
i += 1;
}
 
return 0;
}</langsyntaxhighlight>
Sample output:
<pre> is balanced
Line 167 ⟶ 791:
[][[[][[]]]]][][ is unbalanced
[[][[[[][]]][][]]] is balanced</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<syntaxhighlight lang="algol68"># generates a string of random opening and closing brackets. The number of #
# each type of brackets is speccified in length #
PROC get brackets = ( INT length ) STRING:
BEGIN
INT result length = length * 2;
[ 1 : result length ]CHAR result;
# initialise the brackets to all open brackets #
FOR char pos TO result length DO result[ char pos ] := "[" OD;
# set half of the brackets to close brackets #
INT close count := 0;
WHILE close count < length
DO
INT random pos = 1 + ENTIER ( next random * result length );
IF result[ random pos ] = "["
THEN
close count +:= 1;
result[ random pos ] := "]"
FI
OD;
result
END # get brackets # ;
 
# returns TRUE if the brackets string contains a correctly nested sequence #
# of brackets, FALSE otherwise #
PROC check brackets = ( STRING brackets ) BOOL:
BEGIN
INT depth := 0;
FOR char pos FROM LWB brackets TO UPB brackets
WHILE
IF brackets[ char pos ] = "["
THEN
depth +:= 1
ELSE
depth -:= 1
FI;
depth >= 0
DO
SKIP
OD;
# depth will be 0 if we reached the end of the string and it was #
# correct, non-0 otherwise #
depth = 0
END # check brackets # ;
 
# procedure to test check brackets #
PROC test check brackets = ( STRING brackets ) VOID:
print( ( ( brackets
+ ": "
+ IF check brackets( brackets ) THEN "ok" ELSE "not ok" FI
)
, newline
)
) ;
 
# test the bracket generation and checking PROCs #
test check brackets( get brackets( 0 ) );
FOR length TO 12
DO
TO 2
DO
test check brackets( get brackets( length ) )
OD
OD</syntaxhighlight>
{{out}}
<pre>
: ok
[]: ok
][: not ok
[][]: ok
[]][: not ok
[]]][[: not ok
[[]][]: ok
]]][[][[: not ok
[[][[]]]: ok
[][]]][[][: not ok
[[[[]]]][]: ok
]][]]][[[[][: not ok
[[[][[[]]]]]: ok
[[[]]][[][[]]]: ok
[][[][][][][]]: ok
[]]][][]][[[[]][: not ok
]][]][][[[][][][: not ok
][][]][]][]][[[[[]: not ok
]]][[][][][[[][]][: not ok
[[[[][][]][]]][[]]][: not ok
][]][]]][][][][][[[[: not ok
][][]][[][[[[]][][[]]]: not ok
]][[[]][[[[]]]][[[]]][: not ok
]][][]]][]][][]][[[[[[][: not ok
]]][][][]][][[]][[[][][[: not ok
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
algoritmo
 
braizq="[", brader="]" // bug de Hopper :(
largo de datos=0
preparar datos (DATA_BRACKET)
obtener tamaño de datos, menos '1', guardar en 'largo de datos'
iterar
bra="", obtener dato, guardar en 'bra'
i=1, b=0, error_pos=""
iterar grupo( ++i, #( i<=len(bra) && is not neg (b) ),\
#( b += ((bra[i]==braizq) - (bra[i]==brader)) )\
#( error_pos = cat(replicate(" ",i-1),"^\n") ) )
solo si ( #(is pos(b)),\
#( error_pos=cat(cat(" ",error_pos),"(missing closed bracket)\n\n")))
solo si ( #(is neg(b)),\
#( error_pos=cat(error_pos,"(extra closed bracket)\n\n")))
 
imprimir ( #( rpad(" ",40,bra) ), ": ", \
solo si (b, "un"), "balanced\n",\
solo si (b, error_pos) )
mientras ' largo de datos-- '
terminar
 
subrutinas
 
DATA_BRACKET:
datos ("","[ [ ] [ [[] ][] ] [[]] ]","[[ ][[[ ][ ]]",\
"[][][[]]][","][[]][] [[[]]] [][]","[][] [][][[]]",\
"[ a-b * [c/d] + [[10 * sin 30 ]-1] ]",\
"[ a-b * [c/d] + [[10 * sin 30 ]]-1] ]")
back
</syntaxhighlight>
{{out}}
<pre>
: balanced
[ [ ] [ [[] ][] ] [[]] ] : balanced
[[ ][[[ ][ ]] : unbalanced
^
(missing closed bracket)
 
[][][[]]][ : unbalanced
^
(extra closed bracket)
 
][[]][] [[[]]] [][] : unbalanced
^
(extra closed bracket)
 
[][] [][][[]] : balanced
[ a-b * [c/d] + [[10 * sin 30 ]-1] ] : balanced
[ a-b * [c/d] + [[10 * sin 30 ]]-1] ] : unbalanced
^
(extra closed bracket)
</pre>
 
=={{header|ANTLR}}==
Line 173 ⟶ 958:
<br clear=both>
==={{header|Java}}===
<syntaxhighlight lang="text">
grammar balancedBrackets ;
 
Line 193 ⟶ 978:
NEWLINE : '\r'? '\n'
;
</syntaxhighlight>
</lang>
Produces:
<pre>
Line 211 ⟶ 996:
input is: []line 1:2 missing NEWLINE at ']'
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
 
These are a function to generate a random string of N brackets of each type,
and a function to check whether a given string of brackets is balanced.
 
<syntaxhighlight lang="apl">gen ← (⊂+?+)⍨ ⌷ ('[]'/⍨⊢)
bal ← (((|≡⊢)+\) ∧ 0=+/)(+⌿1 ¯1×[1]'[]'∘.=⊢)</syntaxhighlight>
 
Sample run for 0..N..10:
 
<syntaxhighlight lang="apl"> ↑{br←gen ⍵ ⋄ br,': ',(1+bal br)⊃'Bad' 'Good'}¨0,⍳10
: Good
[]: Good
][][: Bad
[][[]]: Good
[]]][[][: Bad
][][[[[]]]: Bad
][][][][][[]: Bad
][[[][[][]][]]: Bad
[[][[]]][[[[]]]]: Good
[[[]][[[[][]][]]]]: Good
]][][][][[][][]][[[]: Bad</syntaxhighlight>
 
=={{header|AppleScript}}==
 
{{trans|JavaScript}}
(ES6 functionally composed version)
 
<syntaxhighlight lang="applescript">-- CHECK NESTING OF SQUARE BRACKET SEQUENCES ---------------------------------
 
-- Zero-based index of the first problem (-1 if none found):
 
-- imbalance :: String -> Integer
on imbalance(strBrackets)
script
on errorIndex(xs, iDepth, iIndex)
set lngChars to length of xs
if lngChars > 0 then
set iNext to iDepth + cond(item 1 of xs = "[", 1, -1)
if iNext < 0 then -- closing bracket unmatched
iIndex
else
if lngChars > 1 then -- continue recursively
errorIndex(items 2 thru -1 of xs, iNext, iIndex + 1)
else -- end of string
cond(iNext = 0, -1, iIndex)
end if
end if
else
cond(iDepth = 0, -1, iIndex)
end if
end errorIndex
end script
result's errorIndex(characters of strBrackets, 0, 0)
end imbalance
 
-- TEST ----------------------------------------------------------------------
 
-- Random bracket sequences for testing
-- brackets :: Int -> String
on randomBrackets(n)
-- bracket :: () -> String
script bracket
on |λ|(_)
cond((random number) < 0.5, "[", "]")
end |λ|
end script
intercalate("", map(bracket, enumFromTo(1, n)))
end randomBrackets
 
on run
set nPairs to 6
-- report :: Int -> String
script report
property strPad : concat(replicate(nPairs * 2 + 4, space))
on |λ|(n)
set w to n * 2
set s to randomBrackets(w)
set i to imbalance(s)
set blnOK to (i = -1)
set strStatus to cond(blnOK, "OK", "problem")
set strLine to "'" & s & "'" & ¬
(items (w + 2) thru -1 of strPad) & strStatus
set strPointer to cond(blnOK, ¬
"", linefeed & concat(replicate(i + 1, space)) & "^")
intercalate("", {strLine, strPointer})
end |λ|
end script
linefeed & ¬
intercalate(linefeed, ¬
map(report, enumFromTo(1, nPairs))) & linefeed
end run
 
 
-- GENERIC FUNCTIONS ---------------------------------------------------------
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
-- Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
 
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
script append
on |λ|(a, b)
a & b
end |λ|
end script
if length of xs > 0 and class of (item 1 of xs) is string then
set empty to ""
else
set empty to {}
end if
foldl(append, empty, xs)
end concat
 
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
 
-- replicate :: Int -> a -> [a]
on replicate(n, a)
set out to {}
if n < 1 then return out
set dbl to {a}
repeat while (n > 1)
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate
 
-- Value of one of two expressions
-- cond :: Bool -> a -> b -> c
on cond(bln, f, g)
if bln then
set e to f
else
set e to g
end if
if class of e is handler then
mReturn(e)'s |λ|()
else
e
end if
end cond
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn</syntaxhighlight>
'''Sample output:'''
<pre>'][' problem
^
'[][[' problem
^
'[[][]]' OK
'][][[][[' problem
^
'[]][][[][]' problem
^
'[[[][]]]][][' problem
^</pre>
 
=={{header|ARM Assembly}}==
<syntaxhighlight lang="arm_assembly">
.data
 
balanced_message:
.ascii "OK\n"
 
unbalanced_message:
.ascii "NOT OK\n"
 
 
.text
 
.equ balanced_msg_len, 3
.equ unbalanced_msg_len, 7
 
 
BalancedBrackets:
 
mov r1, #0
mov r2, #0
mov r3, #0
 
process_bracket:
ldrb r2, [r0, r1]
 
cmp r2, #0
beq evaluate_balance
 
cmp r2, #'['
addeq r3, r3, #1
cmp r2, #']'
subeq r3, r3, #1
 
cmp r3, #0
blt unbalanced
 
add r1, r1, #1
b process_bracket
 
evaluate_balance:
cmp r3, #0
beq balanced
 
unbalanced:
ldr r1, =unbalanced_message
mov r2, #unbalanced_msg_len
b display_result
 
balanced:
ldr r1, =balanced_message
mov r2, #balanced_msg_len
 
display_result:
mov r7, #4
mov r0, #1
svc #0
 
mov pc, lr
 
</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">isBalanced: function [s][
cnt: 0
loop split s [ch][
if? ch="]" [
cnt: cnt-1
if cnt<0 -> return false
]
else [
if ch="[" -> cnt: cnt+1
]
]
 
cnt=0
]
loop 1..10 'i [
str: join map 0..(2*i)-1 [x][ sample ["[" "]"]]
prints str
 
if? isBalanced str -> print " OK"
else -> print " Not OK"
]</syntaxhighlight>
 
{{out}}
 
<pre>[] OK
[[[] Not OK
[][][] OK
[[][[]]] OK
[[[[[][[]] Not OK
[[]][[[]]][] OK
][[]][][[[[[[] Not OK
]][[]]][[[]]][]] Not OK
][[[[[[]]][]]][][] Not OK
[]][][][]]][[[[][][] Not OK</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">; Generate 10 strings with equal left and right brackets
Loop, 5
{
Line 244 ⟶ 1,356:
}
Return "OK"
}</langsyntaxhighlight>
Output:
<pre>
Line 260 ⟶ 1,372:
 
A second example repeatedly replacing []:
<langsyntaxhighlight AutoHotkeylang="autohotkey">Loop, 5
{
B = %A_Index%
Line 282 ⟶ 1,394:
StringReplace, Str, Str,[],,All
Return Str ? "False" : "True"
}</langsyntaxhighlight>
Sample output:
<pre>
Line 298 ⟶ 1,410:
 
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">
<lang AutoIt>
#include <Array.au3>
Local $Array[1]
Line 327 ⟶ 1,439:
Return 1
EndFunc
</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
print isbb("[]")
Line 352 ⟶ 1,464:
return (s==0)
}
</syntaxhighlight>
</lang>
Output:
<pre>1
Line 360 ⟶ 1,472:
1
0</pre>
 
=={{header|BaCon}}==
<syntaxhighlight lang="bacon">FOR len = 0 TO 18 STEP 2
 
str$ = ""
DOTIMES len
str$ = str$ & CHR$(IIF(RANDOM(2) = 0, 91, 93))
DONE
 
status = 0
FOR x = 1 TO LEN(str$)
IF MID$(str$, x, 1) = "[" THEN
INCR status
ELSE
DECR status
FI
IF status < 0 THEN BREAK
NEXT
 
IF status = 0 THEN
PRINT "OK: ", str$
ELSE
PRINT "BAD: ", str$
ENDIF
NEXT</syntaxhighlight>
{{out}}
<pre>OK:
OK: []
BAD: ]][]
OK: [[]][]
OK: [[[][]]]
BAD: ][[][]]][]
BAD: ]][[]][[[][[
OK: [[][][][][][]]
BAD: [[[]]]][[[[[][]]
BAD: ][][][][[[[[][]]][</pre>
 
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION checkBrackets% (brackets AS STRING)
 
<lang qbasic>DECLARE FUNCTION checkBrackets% (brackets AS STRING)
DECLARE FUNCTION generator$ (length AS INTEGER)
 
Line 420 ⟶ 1,567:
NEXT
generator$ = xx$
END FUNCTION</langsyntaxhighlight>
{{out}}
 
<pre> [][[][][]] OK
Sample output:
[][[][][]] OK
][[[]] NOT OK
[][] OK
Line 434 ⟶ 1,580:
][][[[]] NOT OK
][[]][ NOT OK
OK</pre>
 
==={{header|Applesoft BASIC}}===
The [[#MSX_BASIC|MSX BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
{{trans|Yabasic}}
<syntaxhighlight lang="basic256">s$ = "[[]][]"
print s$; " = ";
 
if not check_brackets(s$) then print "not ";
print "ok"
end
 
function check_brackets(s$)
level = 0
for i = 1 to length(s$)
c$ = mid(s$, i, 1)
begin case
case c$ = "["
level = level + 1
case c$ = "]"
level = level - 1
if level < 0 then exit for
end case
next i
return level = 0
end function</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
The [[#MSX_BASIC|MSX BASIC]] solution works without any changes.
 
==={{header|Commodore BASIC}}===
Based on ZX Spectrum BASIC implementation
<syntaxhighlight lang="basic">10 PRINT CHR$(147): REM CLEAR SCREEN
20 FOR N=1 TO 7
30 READ S$
40 IF S$="" THEN PRINT"(EMPTY)";: GOTO 60
50 PRINT S$;
60 PRINT TAB(20);
70 GOSUB 1000
80 NEXT N
90 END
100 REM ********************************
1000 S = 0
1010 FOR K=1 TO LEN(S$)
1020 C$ = MID$(S$,K,1)
1030 IF C$="[" THEN S = S+1
1040 IF C$="]" THEN S = S-1
1050 IF S<0 THEN PRINT "NOT OK": RETURN
1060 NEXT K
1070 IF S=0 THEN PRINT "OK": RETURN
1090 PRINT "NOT OK"
1100 RETURN
2000 DATA , [], ][, [][], ][][, [[][]], []][[]</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#MSX_BASIC|MSX BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
Based on Commodore BASIC implementation
{{works with|MSX BASIC|any}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS : rem 10 HOME for Applesoft BASIC
110 FOR N = 1 TO 7
120 READ S$
130 IF S$ = "" THEN PRINT"(EMPTY)";: GOTO 150
140 PRINT S$;
150 PRINT TAB(9);
160 GOSUB 190
170 NEXT N
180 END
190 S = 0
200 FOR K = 1 TO LEN(S$)
210 C$ = MID$(S$,K,1)
220 IF C$ = "[" THEN S = S+1
230 IF C$ = "]" THEN S = S-1
240 IF S < 0 THEN PRINT "NOT OK": RETURN
250 NEXT K
260 IF S = 0 THEN PRINT " OK": RETURN
270 PRINT "NOT OK"
280 RETURN
290 DATA "", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"</syntaxhighlight>
{{out}}
<pre>(EMPTY) OK
[] OK
][ NOT OK
[][] OK
][][ NOT OK
[[][]] OK
[]][[] NOT OK</pre>
 
==={{header|True BASIC}}===
{{trans|Run BASIC}}
<syntaxhighlight lang="qbasic">DIM brk$(10)
LET brk$(1) = "[[[][]]]"
LET brk$(2) = "[[[]][[[][[][]]]]]"
LET brk$(3) = "][][]][["
LET brk$(4) = "[][][]"
LET brk$(5) = "[][]][]][[]]][[["
LET brk$(6) = "]][[[[]]]][]]][[[["
LET brk$(7) = "[[][[[]]][]]"
LET brk$(8) = "[]][][][[[]]"
LET brk$(9) = "][]][["
LET brk$(10) = "[]][][][[]"
 
FOR i = 1 TO 7
LET b$ = brk$(i)
DO WHILE POS(b$,"[]") <> 0
LET x = POS(b$,"[]")
IF x > 0 THEN LET b$ = (b$)[1:x-1] & (b$)[x+2:maxnum]
LOOP
IF TRIM$(b$) = "" THEN
PRINT " OK ";
ELSE
PRINT "Not OK ";
END IF
PRINT brk$(i)
NEXT i
END</syntaxhighlight>
==={{header|uBasic/4tH}}===
<syntaxhighlight lang="qbasic">@(0) := "[]][][][[]"
@(1) := "[[[][]]]"
@(2) := "[[[]][[[][[][]]]]]"
@(3) := "][][]][["
@(4) := "[][][]"
@(5) := "[][]][]][[]]][[["
@(6) := "]][[[[]]]][]]][[[["
@(7) := "[[][[[]]][]]"
@(8) := "[]][][][[[]]"
@(9) := "][]][["
 
For x = 0 To 9
Print Using "(_#)";x,Show (@(x));Tab (30);
Do While (Set (a, Find(@(x), "[]")) < 0) = 0
@(x) = Join (Clip (@(x), Len (@(x)) - a), Chop (@(x), a + 2))
Loop
Print "is";Show (Iif (Len (@(x))," not "," "));"balanced"
Next</syntaxhighlight>
{{Out}}
<pre>( 0) []][][][[] is not balanced
( 1) [[[][]]] is balanced
( 2) [[[]][[[][[][]]]]] is balanced
( 3) ][][]][[ is not balanced
( 4) [][][] is balanced
( 5) [][]][]][[]]][[[ is not balanced
( 6) ]][[[[]]]][]]][[[[ is not balanced
( 7) [[][[[]]][]] is balanced
( 8) []][][][[[]] is not balanced
( 9) ][]][[ is not balanced
 
0 OK, 0:472 </pre>
 
=={{header|Batch File}}==
Uses the rewrite rule <code>"[]" -> null</code> to check if brackets are balanced.
<syntaxhighlight lang="dos">:: Balanced Brackets Task from Rosetta Code
:: Batch File Implementation
@echo off
setlocal enabledelayedexpansion
 
set "num_pairs=10"
set "num_strings=10"
 
:: the main thing
for /l %%s in (1, 1, %num_strings%) do (
call :generate
call :check
)
echo(
pause
exit /b 0
 
:: generate strings of brackets
:generate
set "string="
rem put %num_pairs% number of "[" in string
for /l %%c in (1, 1, %num_pairs%) do set "string=!string!["
rem put %num_pairs% number of "]" in random spots of string
set "ctr=%num_pairs%"
for /l %%c in (1, 1, %num_pairs%) do (
set /a "rnd=!random! %% (!ctr! + 1)"
for %%x in (!rnd!) do (
set "left=!string:~0,%%x!"
set "right=!string:~%%x!"
)
set "string=!left!]!right!"
set /a "ctr+=1"
)
goto :EOF
 
:: check for balance
:check
set "new=%string%"
:check_loop
if "%new%" equ "" (
echo(
echo(%string% is Balanced.
goto :EOF
) else if "%old%" equ "%new%" ( %== unchangeable already? ==%
echo(
echo(%string% is NOT Balanced.
goto :EOF
)
rem apply rewrite rule "[]" -> null
set "old=%new%"
set "new=%old:[]=%"
goto check_loop</syntaxhighlight>
{{out}}
<pre>[][][[[[]]][]]]][[][ is NOT Balanced.
 
][[]][][][[]]][[[[]] is NOT Balanced.
 
[[[][]][][]][[][][]] is Balanced.
 
][[[[[]]][][][][][]] is NOT Balanced.
 
]][][[][]]]][]][[[[[ is NOT Balanced.
 
[[[][[][][[]]]][][]] is Balanced.
 
][]]][[]][[][[][[]][ is NOT Balanced.
 
[[[][[][]][[[]]][]]] is Balanced.
 
]][]]][[[][]][[][[[] is NOT Balanced.
 
][[]][][[]][][]][[][ is NOT Balanced.
 
Press any key to continue . . .</pre>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic">FOR x%=1 TO 10
test$=FNgenerate(RND(10))
PRINT "Bracket string ";test$;" is ";FNvalid(test$)
Line 465 ⟶ 1,849:
IF count%<0 THEN ="not OK."
NEXT x%
="OK."</langsyntaxhighlight>
<pre>Bracket string [[[][]]] is OK.
Bracket string [[[]][[[][[][]]]]] is OK.
Line 480 ⟶ 1,864:
{{works with|befungee}}
This code implements the second part of the task: it reads from standard input an arbitrary string of opening and closing brackets, and checks whether it's balanced or not.
<langsyntaxhighlight Befungelang="befunge">v > "KO TON" ,,,,,, v
> ~ : 25*- #v_ $ | > 25*, @
> "KO" ,, ^
Line 486 ⟶ 1,870:
> \ : 1991+*+- #v_v
\ $
^ < <$<</langsyntaxhighlight>
 
=={{header|BQN}}==
 
Balanced brackets are a monoid with the following presentation:
<pre>⟨l, r | l·r = e⟩</pre>
 
This allows for a particular simple implementation:
 
<syntaxhighlight lang="bqn">Gen ← (•rand.Deal⊏⥊⟜"[]") 2⊸×
Bal ← {
Mul ← {a‿b𝕊x‿y: ⟨a+0⌈x-b, y+0⌈b-x⟩}
0‿0 ≡ 0‿0("]["⊸=⊸Mul)´𝕩
}</syntaxhighlight>
 
{{out}}
<pre>
(⊢≍Bal¨) Gen¨5⥊3
┌─
╵ "]][][[" "[][][]" "][][[]" "[[][]]" "][[][]"
0 1 0 1 0
</pre>
([https://mlochbaum.github.io/BQN/try.html#code=R2VuIOKGkCAo4oCicmFuZC5EZWFs4oqP4qWK4p+cIltdIikgMuKKuMOXCkJhbCDihpAgewogIE11bCDihpAge2HigL9i8J2VinjigL95OiDin6hhKzDijIh4LWIsIHkrMOKMiGIteOKfqX0KICAw4oC/MCDiiaEgMOKAvzAoIl1bIuKKuD3iirhNdWwpwrTwnZWpCn0KCijiiqLiiY1CYWzCqCkgR2Vuwqg14qWKMwo= online REPL])
 
=={{header|Bracmat}}==
Bracmat has no 'random' function, so the shuffle is a bit improvised. A variable <code>someNumber</code> is initialised with a big number is repeatedly divided by the number of '['s in the test string until zero. The remainders are used as index to partition and swap the first half of the test string. Then the second half and first half are also swapped. The test whether the test string is balanced is simple, but not very efficient.
<langsyntaxhighlight lang="bracmat">( (bal=|"[" !bal "]" !bal)
& ( generate
= a j m n z N S someNumber
Line 516 ⟶ 1,923:
& !L+1:<11:?L
)
);</langsyntaxhighlight>
Output:
<pre>:Balanced
Line 531 ⟶ 1,938:
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">string.prototype.balanced? = {
brackets = []
balanced = true
Line 561 ⟶ 1,968:
{ p "#{test} is balanced" }
{ p "#{test} is not balanced" }
}</langsyntaxhighlight>
 
Output:
Line 576 ⟶ 1,983:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Line 618 ⟶ 2,025:
while(n<9) doSeq(n++);
return 0;
}</langsyntaxhighlight>result:<syntaxhighlight lang="text">'': True
'[]': True
']][[': False
Line 626 ⟶ 2,033:
']]]][[[]][[[': False
']]]]]][][[[[[[': False
'[][]][[][[[]]][]': False</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
 
Line 670 ⟶ 2,077:
}
}
}</langsyntaxhighlight>
Sample output:
<pre>"" is balanced.
Line 681 ⟶ 2,088:
"[]]][][]][[][[" is not balanced.
"[]]][]]][[][[][[" is not balanced.</pre>
<langsyntaxhighlight lang="csharp">
// simple solution
string input = Console.ReadLine();
Line 706 ⟶ 2,113:
Console.WriteLine("Not Okay");
 
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <string>
Line 741 ⟶ 2,148:
std::cout << (balanced(s) ? " ok: " : "bad: ") << s << "\n";
}
}</langsyntaxhighlight>
Output:
<pre> ok:
Line 753 ⟶ 2,160:
bad: [[]]]][]][[][[[]
</pre>
 
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">import com.vasileff.ceylon.random.api {
platformRandom,
Random
}
"""Run the example code for Rosetta Code ["Balanced brackets" task] (http://rosettacode.org/wiki/Balanced_brackets)."""
shared void run() {
value rnd = platformRandom();
for (len in (0..10)) {
value c = generate(rnd, len);
print("``c.padTrailing(20)`` - ``if (balanced(c)) then "OK" else "NOT OK" ``");
}
}
 
String generate(Random rnd, Integer count)
=> if (count == 0) then ""
else let(length = 2*count,
brackets = zipEntries(rnd.integers(length).take(length),
"[]".repeat(count))
.sort((a,b) => a.key<=>b.key)
.map(Entry.item))
String(brackets);
 
Boolean balanced(String input)
=> let (value ints = { for (c in input) if (c == '[') then 1 else -1 })
ints.filter((i) => i != 0)
.scan(0)(plus<Integer>)
.every((i) => i >= 0);</syntaxhighlight>
 
Output:
 
<pre> - OK
[] - OK
][[] - NOT OK
][[]][ - NOT OK
[]]][[][ - NOT OK
[[[][][]]] - OK
[[][]][[]][] - OK
[[][[[]]][]][] - OK
]][[][][[][]][[] - NOT OK
[[]]]]]]][[[[][][[ - NOT OK
[[]][[]][[]]]][[[]][ - NOT OK</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn gen-brackets [n]
(->> (concat (repeat n \[) (repeat n \]))
shuffle
Line 768 ⟶ 2,218:
(when (= (peek stack) \[)
(recur coll (pop stack))))
(zero? (count stack)))))</langsyntaxhighlight>
 
There are other ways to express the <code>balanced?</code> function.
 
* We can use <code>reduce</code> to consume the sequence:
:<syntaxhighlight lang="clojure">(defn balanced? [s]
(empty?
(reduce
(fn [stack first]
(case first
\[ (conj stack \[)
\] (if (seq stack)
(pop stack)
(reduced [:UNDERFLOW]))))
'()
s)))</syntaxhighlight>
 
* Only <code>[</code>s are put on the stack. We can just count the unmatched ones.
:<syntaxhighlight lang="clojure">(defn balanced? [s]
(let [opens-closes (->> s
(map {\[ 1, \] -1})
(reductions + 0))]
(and (not-any? neg? opens-closes) (zero? (last opens-closes))))) </syntaxhighlight>
 
Output:
Line 774 ⟶ 2,246:
<pre>user> (->> (range 10)
(map gen-brackets ,)
(map (juxt identity balanced?) ,) vec)
vec)
[["" true]
["[]" true]
Line 785 ⟶ 2,258:
["][][[]]][[][][][" nil]
["][][]]][]][[[][[[]" nil]</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% This program needs the random number generator from
% "misc.lib" that comes with PCLU.
 
shuffle = proc [T: type] (a: array[t])
aT = array[t]
for i: int in int$from_to_by(aT$size(a)-1,0,-1) do
x: int := aT$low(a) + i
y: int := aT$low(a) + random$next(i+1)
temp: T := a[x]
a[x] := a[y]
a[y] := temp
end
end shuffle
 
brackets = proc (n: int) returns (string)
br: array[char] := array[char]$[]
for i: int in int$from_to(1,2*n) do
if i<=n then array[char]$addh(br, '[')
else array[char]$addh(br, ']')
end
end
shuffle[char](br)
return(string$ac2s(br))
end brackets
 
balanced = proc (br: string) returns (bool)
depth: int := 0
for c: char in string$chars(br) do
if c='[' then depth := depth + 1
elseif c=']' then depth := depth - 1
end
if depth<0 then return(false) end
end
return(depth = 0)
end balanced
 
start_up = proc ()
po: stream := stream$primary_output()
d: date := now()
random$seed(d.second + 60*(d.minute + 60*d.hour))
for size: int in int$from_to(0, 10) do
b: string := brackets(size)
stream$puts(po, "\"" || b || "\": ")
if balanced(b) then
stream$putl(po, "balanced")
else
stream$putl(po, "not balanced")
end
end
end start_up</syntaxhighlight>
{{out}}
<pre>"": balanced
"[]": balanced
"[][]": balanced
"[[]][]": balanced
"[[]][[]]": balanced
"][[]][][[]": not balanced
"[][][]][]][[": not balanced
"][]][]]][][[[[": not balanced
"[][[]][][][[[]]]": balanced
"][[][][]][[]][][[]": not balanced
"]]]][[[[]][][][[][[]": not balanced</pre>
 
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. test-balanced-brackets.
 
Line 885 ⟶ 2,423:
.
 
END PROGRAM check-if-balanced.</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
isBalanced = (brackets) ->
openCount = 0
Line 904 ⟶ 2,442:
for brackets in bracketsCombinations 4
console.log brackets, isBalanced brackets
</syntaxhighlight>
</lang>
output
<syntaxhighlight lang="text">
> coffee balanced.coffee
[[[[ false
Line 924 ⟶ 2,462:
]]][ false
]]]] false
</syntaxhighlight>
</lang>
 
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun string-of-brackets (n)
(let* ((result (make-stringlen (* 2 n)))
(res (openingmake-string nlen))
(closingopening (/ len n2))
(dotimes (iclosing (*/ 2len n2)) result)
(setfdotimes (arefi resultlen ires)
(setf (aref res (condi)
(cond ((zerop opening) #\])
((zerop closing) #\[)
(t (if (= (random 2) 0)
(progn (decf opening) #\[)
(progn (decf closing) #\]))))))))
 
(defun balancedp (string)
Line 956 ⟶ 2,493:
(let ((s (string-of-brackets i)))
(format t "~3A: ~A~%" (balancedp s) s))))
</syntaxhighlight>
</lang>
 
Output:
Line 973 ⟶ 2,510:
NIL: ]][[[[][]][[[[]]]]
</pre>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE Brackets;
IMPORT StdLog, Args, Stacks (* See Task Stacks *);
Line 1,036 ⟶ 2,574:
 
END Brackets.
</syntaxhighlight>
</lang>
Execute: ^Q Brackets.Do [] [][] [[][]] ][ ][][ []][[]~<br/>
Output:
Line 1,047 ⟶ 2,585:
[]][[]:> $FALSE
</pre>
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">def generate(n : Int)
(['[',']'] * n).shuffle.join # Implicit return
end
 
def is_balanced(str : String)
count = 0
str.each_char do |ch|
case ch
when '['
count += 1
when ']'
count -= 1
if count < 0
return false
end
else
return false
end
end
count == 0
end
 
10.times do |i|
str = generate(i)
puts "#{str}: #{is_balanced(str)}"
end</syntaxhighlight>
 
Output:
 
<pre>: true
[]: true
][][: false
][[]][: false
[]]][][[: false
][[]][[[]]: false
[]]][[[]][[]: false
[[][[[][]]]]][: false
[[[]]]][][][][[]: false
[[][][]][][[]][][]: true</pre>
 
=={{header|D}}==
===Standard Version===
D standard library has a [http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#balancedParens function] for this.
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.random, std.range;
 
void main() {
Line 1,057 ⟶ 2,637:
writeln(s.balancedParens('[', ']') ? " OK: " : "bad: ", s);
}
}</langsyntaxhighlight>
{{out}}
<pre> OK: []
Line 1,069 ⟶ 2,649:
 
===Imperative Version===
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.range, std.algorithm;
 
bool isBalanced(in string txt) pure nothrow {
Line 1,092 ⟶ 2,672:
writeln(s.isBalanced ? " OK " : "Bad ", s);
}
}</langsyntaxhighlight>
The output is similar.
 
===Functional Style===
{{trans|Haskell}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.range, std.algorithm;
 
bool isBalanced(in string s, in stringchar[2] pars="[]") pure nothrow @safe @nogc {
bool bal(in string t, in int nb = 0) pure nothrow @safe @nogc {
if (!nb && t.empty) return true;
if (t.empty || nb < 0) return false;
Line 1,115 ⟶ 2,695:
writeln(s.isBalanced ? " OK " : "Bad ", s);
}
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
 
<langsyntaxhighlight Delphilang="delphi">procedure Balanced_Brackets;
 
var BracketsStr : string;
Line 1,149 ⟶ 2,729:
writeln(BracketsStr+': not OK');
end;
end;</langsyntaxhighlight>
 
<pre>
Line 1,164 ⟶ 2,744:
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">matching?:
swap 0
for c in chars:
Line 1,182 ⟶ 2,762:
!. matching? "]["
!. matching? "][]["
!. matching? "[]][[]"</langsyntaxhighlight>
{{out}}
<pre>true
Line 1,191 ⟶ 2,771:
false
false</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func balanced code$ .
for i to len code$
h$ = substr code$ i 1
if h$ = "["
br += 1
elif h$ = "]"
br -= 1
.
if br < 0
return 0
.
.
return if br = 0
.
repeat
inp$ = input
until inp$ = "eof"
print inp$ & "\t" & balanced inp$
.
#
input_data
 
[]
[][]
[[][]]
][
][][
[]][[]
eof
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(define (balance str)
(for/fold (closed 0) ((par str))
#:break (< closed 0 ) => closed
(+ closed
(cond
((string=? par "[") 1)
((string=? par "]") -1)
(else 0)))))
(define (task N)
(define str (list->string (append (make-list N "[") (make-list N "]"))))
(for ((i 10))
(set! str (list->string (shuffle (string->list str))))
(writeln (if (zero? (balance str)) '👍 '❌ ) str)))
 
(task 4)
 
❌ "[]]][[]["
❌ "]][][[[]"
❌ "][[[]]]["
👍 "[][[[]]]"
❌ "]][[][]["
❌ "][][[[]]"
👍 "[][][[]]"
❌ "]][[][[]"
❌ "[[]]][[]"
❌ "[[][]]]["
</syntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BalancedBrackets {
Boolean balanced(String text) {
Int depth = 0;
for (Char ch : text) {
switch (ch, depth) {
case ('[', _):
++depth;
break;
case (']', 0):
return False;
case (']', _):
--depth;
break;
}
}
return depth==0;
}
 
@Inject Console console;
void run() {
String[] tests =
[
"[]",
"[][]",
"[]][[]",
"[[[]][]]",
"][[[[]][]]",
"[[[]][[]][]]",
"]][[]][[[[][]]",
"[[]]]][]][[][[[]",
];
Int longest = tests.map(s -> s.size).reduce(0, (max, len) -> max.maxOf(len));
for (String test : tests) {
console.print($"{test}{' ' * (longest-test.size)} {balanced(test) ? "OK" : "NOT OK"}");
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
[] OK
[][] OK
[]][[] NOT OK
[[[]][]] OK
][[[[]][]] NOT OK
[[[]][[]][]] OK
]][[]][[[[][]] NOT OK
[[]]]][]][[][[[] NOT OK
</pre>
 
=={{header|Elena}}==
ELENA 6.x :
<lang elena>#define system.
<syntaxhighlight lang="elena">// Generate a string with N opening brackets ("[") and N closing brackets ("]"), in some arbitrary order.
#define system'routines.
// Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order),
#define extensions.
// none of which mis-nest.
 
import system'routines;
#symbol randomBrackets = &&:aLength
import extensions;
[
import extensions'text;
#var aBrackets := Array new &type'length:aLength &function: &&:i[ CharValue new:91 ] + Array new &type'length:aLength &function: &&:i[ CharValue new:93 ].
 
randomBrackets(len)
{
if (0 == len)
{
^emptyString
}
else
{
var brackets :=
Array.allocate(len).populate::(i => $91)
+
Array.allocate(len).populate::(i => $93);
 
brackets := brackets.randomize(len * 2);
 
^ brackets.summarize(new StringWriter()).toString()
}
}
 
extension op
{
get isBalanced()
{
var counter := new Integer(0);
self.seekEach::(ch => counter.append((ch==$91).iif(1,-1)) < 0);
randomControl randomize:(aLength * 2) &array:aBrackets.
^ (0 == counter)
^ Summing new:(String new) foreach:aBrackets Literal.
}
].
}
 
public program()
#symbol isBalanced = &&:aLiteral
{
[
for(int len := 0; len < 9; len += 1)
#var aCounter := Integer new:0.
{
var str := randomBrackets(len);
 
console.printLine("""",str,"""",str.isBalanced ? " is balanced" : " is not balanced")
control foreach:aLiteral &until: &&:aChar [ aCounter append:(aChar => "[" ? [ 1 ] "]" ? [ -1 ]) < 0 ].
};
 
console.readChar()
^ (0 == aCounter).
}</syntaxhighlight>
].
{{out}}
<pre>
"" is balanced
"[]" is balanced
"][[]" is not balanced
"[[[]]]" is balanced
"][[[]]][" is not balanced
"[]]]][[[][" is not balanced
"[[]][][[]]][" is not balanced
"[][]]]]][[[[][" is not balanced
"][]]][[[[][[][]]" is not balanced
"][]][][[]]][[[]][[" is not balanced
</pre>
 
=={{header|Elixir}}==
#symbol program =
{{trans|Erlang}}
[
{{works with|Elixir|1.1}}
control from:0 &till:10 &do: &&:aLength
<syntaxhighlight lang="elixir">defmodule Balanced_brackets do
[
def task do
#var anStr := randomBrackets:aLength.
Enum.each(0..5, fn n ->
#var balanced := isBalanced:anStr.
brackets = generate(n)
result = is_balanced(brackets) |> task_balanced
IO.puts "#{brackets} is #{result}"
end)
end
defp generate( 0 ), do: []
defp generate( n ) do
for _ <- 1..2*n, do: Enum.random ["[", "]"]
end
def is_balanced( brackets ), do: is_balanced_loop( brackets, 0 )
defp is_balanced_loop( _, n ) when n < 0, do: false
defp is_balanced_loop( [], 0 ), do: true
defp is_balanced_loop( [], _n ), do: false
defp is_balanced_loop( ["[" | t], n ), do: is_balanced_loop( t, n + 1 )
defp is_balanced_loop( ["]" | t], n ), do: is_balanced_loop( t, n - 1 )
defp task_balanced( true ), do: "OK"
defp task_balanced( false ), do: "NOT OK"
end
 
Balanced_brackets.task</syntaxhighlight>
console write:"""" write:anStr write:"""" writeLine:(balanced => true ? [ " is balanced" ] false ? [ " is not balanced" ]).
].
 
{{out}}
console readChar.
<pre>
].
is OK
</lang>
[[ is NOT OK
[][] is OK
]][]][ is NOT OK
[[][][]] is OK
[][[][[]]] is OK
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( balanced_brackets ).
-export( [generate/1, is_balanced/1, task/0] ).
Line 1,260 ⟶ 3,028:
task_balanced( true ) -> "OK";
task_balanced( false ) -> "NOT OK".
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,273 ⟶ 3,041:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function check_brackets(sequence s)
integer level
level = 0
Line 1,317 ⟶ 3,085:
puts(1," NOT OK\n")
end if
end for</langsyntaxhighlight>
 
Sample output:
Line 1,330 ⟶ 3,098:
[]]][[ NOT OK
[][[]] OK
 
=={{header|Excel}}==
===LAMBDA===
 
Binding the names '''bracketReport''' and '''testRandomBrackets''' to the following lambda expressions in the Name Manager of the Excel WorkBook, together with names for their helper functions:
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">bracketReport
=LAMBDA(bracketPair,
LAMBDA(s,
LET(
depths, SCANLCOLS(
LAMBDA(depth, charDelta,
depth + charDelta
)
)(0)(
codesFromBrackets(bracketPair)(
MID(s,
SEQUENCE(1, LEN(s), 1, 1),
1
)
)
),
overClosePosn, IFNA(MATCH(-1, depths, 0), 0),
IF(0 <> overClosePosn,
"Overclosed at char " & (overClosePosn - 1),
IF(0 <> LASTCOL(depths),
"Underclosed by end",
"OK"
)
)
)
)
)
 
 
testRandomBrackets
=LAMBDA(bracketPair,
LAMBDA(n,
LET(
sample, CONCAT(
bracketFromCode(bracketPair)(
RANDARRAY(1, n, -1, 1, TRUE)
)
),
 
APPENDCOLS(sample)(
bracketReport(bracketPair)(sample)
)
)
)
)
 
 
bracketFromCode
=LAMBDA(bracketPairString,
LAMBDA(c,
IF(0 <> c,
IF(0 < c,
MID(bracketPairString, 1, 1),
MID(bracketPairString, 2, 1)
),
"x"
)
)
)
 
 
codesFromBrackets
=LAMBDA(bracketPair,
LAMBDA(s,
LAMBDA(c,
LET(
posn, IFERROR(
FIND(c, bracketPair),
0
),
 
rem, "0 for non-bracket chars, (1, -1) for (open, close)",
IF(0 <> posn,
IF(1 < posn, -1, 1),
0
)
)
)(
MID(s,
SEQUENCE(1, LEN(s), 1, 1),
1
)
)
)
)</syntaxhighlight>
 
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<syntaxhighlight lang="lisp">APPENDCOLS
=LAMBDA(xs,
LAMBDA(ys,
LET(
nx, COLUMNS(xs),
colIndexes, SEQUENCE(1, nx + COLUMNS(ys)),
rowIndexes, SEQUENCE(MAX(ROWS(xs), ROWS(ys))),
 
IFERROR(
IF(nx < colIndexes,
INDEX(ys, rowIndexes, colIndexes - nx),
INDEX(xs, rowIndexes, colIndexes)
),
NA()
)
)
)
)
 
 
CHARSROW
=LAMBDA(s,
MID(s,
SEQUENCE(1, LEN(s), 1, 1),
1
)
)
 
 
HEADCOL
=LAMBDA(xs,
LET(REM, "The first item of each column in xs",
 
INDEX(xs, 1, SEQUENCE(1, COLUMNS(xs)))
)
)
 
 
HEADROW
=LAMBDA(xs,
LET(REM, "The first item of each row in xs",
 
INDEX(
xs,
SEQUENCE(ROWS(xs)),
SEQUENCE(1, 1)
)
)
)
 
 
LASTCOL
=LAMBDA(xs,
INDEX(
xs,
SEQUENCE(ROWS(xs), 1, 1, 1),
COLUMNS(xs)
)
)
 
 
SCANLCOLS
=LAMBDA(op,
LAMBDA(a,
LAMBDA(xs,
APPENDCOLS(a)(
LET(
b, op(a, HEADROW(xs)),
 
IF(2 > COLUMNS(xs),
b,
SCANLCOLS(op)(b)(
TAILROW(xs)
)
)
)
)
)
)
)
 
 
TAILCOL
=LAMBDA(cols,
LET(REM, "The tail of each column in the grid.",
 
INDEX(
cols,
SEQUENCE(ROWS(cols) - 1, 1, 2, 1),
SEQUENCE(1, COLUMNS(cols))
)
)
)
 
 
TAILROW
=LAMBDA(xs,
LET(REM,"The tail of each row in the grid",
n, COLUMNS(xs) - 1,
 
IF(0 < n,
INDEX(
xs,
SEQUENCE(ROWS(xs), 1, 1, 1),
SEQUENCE(1, n, 2, 1)
),
NA()
)
)
)</syntaxhighlight>
 
{{Out}}
The formula in cell B2 defines the pair of values (random bracket sample, and verdict on balance), which populates the range '''B2:C2'''
 
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="3" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=testRandomBrackets("[]")(10)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| style="font-weight:bold" | Random sample
| style="font-weight:bold" | Verdict
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="background-color:#cbcefb" | [[[[x[x]xx
| Underclosed by end
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| [][]x[]]][
| Overclosed at char 8
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| ][x[x]]x]x
| Overclosed at char 1
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| [x[]x[]]xx
| OK
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| x]][[xxx[]
| Overclosed at char 2
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 7
|
| [xxx[[[xxx
| Underclosed by end
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 8
|
| []x[[[xx]x
| Underclosed by end
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 9
|
| xxx[][xxx]
| OK
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 10
|
| ][x]x]]]][
| Overclosed at char 1
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 11
|
| ]xxxxx[x]]
| Overclosed at char 1
|}
 
Or applying the same '''bracketReport''' function to non-random samples, with a different bracket type:
 
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="3" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=bracketReport("()")(C2)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| style="font-weight:bold" | Verdict
| style="font-weight:bold" | Sample
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="background-color:#cbcefb" | OK
| (Non-random) sample string
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| OK
| (Non-random) (sample) string
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| Overclosed at char 1
| )((Non-random) (sample) string)
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| Underclosed by end
| ((Non-random) ((sample) string)
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| Overclosed at char 24
| ((Non-random)) (sample)) string)
|}
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let isBalanced str =
let rec loop count = function
| ']'::_ when count = 0 -> false
Line 1,353 ⟶ 3,441:
for n in 1..10 do
let s = generate n
printfn "\"%s\" is balanced: %b" s (isBalanced s)</langsyntaxhighlight>
 
Output:
Line 1,368 ⟶ 3,456:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: combinators formatting kernel math random sequences strings ;
This code implements the second part of the task: it reads from standard input an arbitrary string of opening and closing brackets, and checks whether it's balanced or not.
IN: rosetta-code.balanced-brackets
<lang Factor>USING: io formatting locals kernel math sequences unicode.case ;
 
: balanced? ( str -- ? )
0 swap [
{
{ CHAR: [ [ 1 + t ] }
{ CHAR: ] [ 1 - dup 0 >= ] }
[ drop t ]
} case
] all? swap zero? and ;
 
: bracket-pairs ( n -- str )
[ "[]" ] replicate "" concat-as ;
 
: balanced-brackets-main ( -- )
5 bracket-pairs randomize dup balanced? "" "not " ?
"String \"%s\" is %sbalanced.\n" printf ;
 
MAIN: balanced-brackets-main</syntaxhighlight>
 
The code below implements only the second part of the task: it reads from standard input an arbitrary string of opening and closing brackets, and checks whether it's balanced or not. Unlike the solution above, this one uses local variables.
<syntaxhighlight lang="factor">USING: io formatting locals kernel math sequences unicode.case ;
IN: balanced-brackets
 
Line 1,390 ⟶ 3,499:
 
readln
balanced</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,434 ⟶ 3,543:
}
}
</syntaxhighlight>
</lang>
 
Output (for n=3):
Line 1,462 ⟶ 3,571:
=={{header|Forth}}==
{{works with|4tH|3.61.1}}
<langsyntaxhighlight lang="forth">include lib/choose.4th ( n1 -- n2)
include lib/ctos.4th ( n -- a 1)
 
Line 1,484 ⟶ 3,593:
; \ evaluate string and print result
 
make[] eval[]</langsyntaxhighlight>
'''Examples''':<pre>[][[]] OK
[[[[]][[ NOT OK
Line 1,498 ⟶ 3,607:
[] OK</pre>
 
=={{header|FORTRANFortran}}==
Please see the compilation and program execution result as comments at the top of this source:
<syntaxhighlight lang="fortran">
<lang FORTRAN>
! $ gfortran -g -O0 -std=f2008 -Wall f.f08 -o f.exe
! $ ./f
Line 1,598 ⟶ 3,707:
end subroutine generate
end program balanced_brackets
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function isBalanced(s As String) As Boolean
If s = "" Then Return True
Dim countLeft As Integer = 0 '' counts number of left brackets so far unmatched
Dim c As String
For i As Integer = 1 To Len(s)
c = Mid(s, i, 1)
If c = "[" Then
countLeft += 1
ElseIf countLeft > 0 Then
countLeft -= 1
Else
Return False
End If
Next
Return countLeft = 0
End Function
 
' checking examples in task description
Dim brackets(1 To 7) As String = {"", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"}
For i As Integer = 1 To 7
Print IIf(brackets(i) <> "", brackets(i), "(empty)"); Tab(10); IIf(isBalanced(brackets(i)), "OK", "NOT OK")
Next
 
' checking 7 random strings of brackets of length 8 say
Randomize
Dim r As Integer '' 0 will signify "[" and 1 will signify "]"
Dim s As String
For i As Integer = 1 To 7
s = Space(8)
For j As Integer = 1 To 8
r = Int(Rnd * 2)
If r = 0 Then
Mid(s, j) = "["
Else
Mid(s, j) = "]"
End If
Next j
Print s; Tab(10); IIf(isBalanced(s), "OK", "NOT OK")
Next i
 
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
Sample output (last 7 lines random) :
{{out}}
<pre>
(empty) OK
[] OK
][ NOT OK
[][] OK
][][ NOT OK
[[][]] OK
[]][[] NOT OK
][]][[[[ NOT OK
[]][][]] NOT OK
][][[[]] NOT OK
[[[[]]]] OK
[][[[[][ NOT OK
][[[]][] NOT OK
[][[[[][ NOT OK
</pre>
 
 
=={{header|FutureBasic}}==
Filters out non-bracket characters prior to evaluation.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn BracketBalance( strWithBracket as CFStringRef ) as CFStringRef
NSInteger i, bracketTracker = 0
CFStringRef result
CFCharacterSetRef bracketSet = fn CharacterSetWithCharactersInString( @"[]" )
CFCharacterSetRef bracketsOnlySet = fn CharacterSetInvertedSet( bracketSet )
CFArrayRef trimmedSArray = fn StringComponentsSeparatedByCharactersInSet( strWithBracket, bracketsOnlySet )
CFStringRef trimmedStr = fn ArrayComponentsJoinedByString( trimmedSArray, @"" )
NSUInteger strLen = len( trimmedStr )
// Empty string, no brackets
if ( strLen == 0 ) then result = @"No brackets" : exit fn
// String with odd number of brackets is unbalanced
if ( strLen mod 2 ) then result = @"Unbalanced" : exit fn
for i = 0 to strLen - 1
CFStringRef bracket = fn StringWithFormat( @"%C", fn StringCharacterAtIndex( trimmedStr, i ) )
if fn StringisEqual( bracket, @"[" ) then bracketTracker++
if fn StringisEqual( bracket, @"]" ) then bracketTracker--
if bracketTracker < 0 then result = @"Unbalanced" : break
next
if bracketTracker == 0 then result = @"Balanced"
end fn = result
 
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"" ) ), @"" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[" ) ), @"[" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"]" ) ), @"]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[]" ) ), @"[]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[[]" ) ), @"[[]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[]]" ) ), @"[]]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[][]" ) ), @"[][]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"][" ) ), @"][" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"][][" ) ), @"][][" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[[]][][][[][]]" ) ), @"[[]][][][[][]]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"][[]][][][]]][[" ) ), @"][[]][][][]]][[" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[[[abc]][[d]]]]]" ) ), @"[[[abc]][[def]]]]]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[[[abc]]][[[[[d]]]]]" ) ), @"[[[abc]]][[[[[def]]]]]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"[][abc]]][[[[[d]]]]]" ) ), @"[][abc]]][[[[[def]]]]]" )
NSLog( @"%12s: %@", fn StringUTF8String( fn BracketBalance( @"The quick brown fox" ) ), @"The quick brown fox" )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
No brackets:
Unbalanced: [
Unbalanced: ]
Balanced: []
Unbalanced: [[]
Unbalanced: []]
Balanced: [][]
Unbalanced: ][
Unbalanced: ][][
Balanced: [[]][][][[][]]
Unbalanced: ][[]][][][]]][[
Unbalanced: [[[abc]][[def]]]]]
Balanced: [[[abc]]][[[[[def]]]]]
Unbalanced: [][abc]]][[[[[def]]]]]
No brackets: The quick brown fox
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=8960fb267af43f0549d2cfe04288a2d4 Click this link to run this code]'''
<syntaxhighlight lang="gambas">'Altered to prevent lines starting with ']' or ending with '[' being generated as they can't work
 
siNumberOfBrackets As Short = 20 'Maximum amount of brackets in a line
siNumberOfLines As Short = 20 'Amount of lines to test
'----
Public Sub Main()
Dim sBrks As String[] = GenerateBrackets() 'Get random array to check
Dim sTemp, sHold, sWork As String 'Working variables
Dim siCount As Short 'Counter
For Each sTemp In sBrks 'For each line in the sBrk array (e.g. '[][][][[[[]][]]]')
sWork = sTemp 'Make sWork = sTemp
Repeat 'Repeat
sHold = sWork 'Make sHold = sWork
sWork = Replace(sWork, "[]", "") 'Remove all brackets that match '[]'
Until sHold = sWork 'If sHold = sWork then there are no more '[]' matches
If sWork = "" Then 'So if all the brackets 'Nested' correctly sWork will be empty
Print " OK "; 'Print 'OK'
Else 'Else they did not all match
Print "NOT OK "; 'So print 'NOT OK'
Endif
For siCount = 1 To Len(sTemp) 'Loop through the line of brackets
Print Mid(sTemp, siCount, 1) & " "; 'Print each bracket + a space to make it easier to read
Next
Print 'Print a new line
Next
End
'----
Public Sub GenerateBrackets() As String[] 'Generates an array of random quantities of '[' and ']'
Dim siQty As New Short[] 'To store the random number (of brackets) to put in a line
Dim sBrk As New String[] 'To store the lines of brackets
Dim siNum, siEnd, siLoop As Short 'Various counters
Dim sTemp As String 'Temp string
Repeat 'Repeat
siNum = Rand(0, siNumberOfBrackets) 'Pick a number between 0 and the total number of brackets requested
If Even(siNum) Then siQty.Add(siNum) 'If the number is even then add the number to siQty
Until siQty.Count = siNumberOfLines 'Keep going until we have the number of lines requested
 
For Each siNum In siQty 'For each number in siQty..(e.g. 6)
Do
siEnd = Rand(0, 1) 'Generate a 0 or a 1
If siEnd = 0 Then sTemp &= "[" 'If '0' then add a '[' bracket
If siEnd = 1 Then sTemp &= "]" 'If '1' then add a ']' bracket
If siNum = 0 Then 'If siNum = 0 then..
sBrk.Add("") 'Add '0' to the array
sTemp = "" 'Clear sTemp
Break 'Exit the Do Loop
Endif
If Len(sTemp) = siNum Then 'If the length of sTemp = the required amount then..
If sTemp Not Begins "]" And sTemp Not Ends "[" Then 'Check to see that sTemp does not start with "]" and does not end with a "["
sBrk.Add(sTemp) 'Add it to the array
sTemp = "" 'Clear sTemp
Break 'Exit the Do Loop
Else 'Else
sTemp = "" 'Clear sTemp
End If 'Try again!
Endif
Loop
Next
 
Return sBrk 'Return the sBrk array
 
End</syntaxhighlight>
Output:
<pre>
NOT OK [ ] ] [ [ ] [ [ ] [ [ [ ] [ [ ] [ ]
NOT OK [ [ [ ] [ [ ] [ [ ] ] [ ] ]
NOT OK [ ] [ [ [ ] ] [ [ ] ] ] [ ] ] [ ] ] ] ]
NOT OK [ [ [ ] [ ] ] ] ] ] [ [ ] ] [ ] [ [ ] ]
NOT OK [ [ ] ] ] ]
NOT OK [ ] ] [ [ ] [ ] [ ]
NOT OK [ [ [ ] [ ]
OK [ ]
NOT OK [ ] ] ] [ ]
NOT OK [ [ ] ] ] ]
OK [ [ [ [ ] ] [ ] [ ] ] ]
OK [ ]
OK [ ] [ ]
NOT OK [ ] ] ] [ ] [ [ [ [ ] [ [ ] [ ]
NOT OK [ ] ] ] [ [ ] ] ] [ ] ] ] ]
NOT OK [ ] ] ] ] ] [ [ ] [ ] ] ] [ ] [ [ [ [ ]
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">Balanced := function(L)
local c, r;
r := 0;
Line 1,636 ⟶ 3,973:
 
Balanced("[[[]][]]]");
# false</langsyntaxhighlight>
 
=={{header|GDScript}}==
{{works with|Godot|4.0}}
 
<syntaxhighlight lang="gdscript">
extends MainLoop
 
func generate_brackets(n: int) -> String:
var brackets: Array[String] = []
 
# Add opening and closing brackets
brackets.resize(2*n)
for i in range(0, 2*n, 2):
brackets[i] = "["
brackets[i+1] = "]"
 
brackets.shuffle()
return "".join(brackets)
 
func is_balanced(str: String) -> bool:
var unclosed_brackets := 0
for c in str:
match c:
"[":
unclosed_brackets += 1
"]":
if unclosed_brackets == 0:
return false
unclosed_brackets -= 1
_:
return false
return unclosed_brackets == 0
 
func _process(_delta: float) -> bool:
randomize()
 
for i in range(6):
var bracket_string := generate_brackets(i)
 
if is_balanced(bracket_string):
print("%sOK" % bracket_string.rpad(13))
else:
print("%sNOT OK" % bracket_string.rpad(11))
 
return true # Exit
</syntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,662 ⟶ 4,045:
 
func testBalanced(s string) {
fmt.PrintfPrint("%s: + ",: s")
var open int:= 0
for i_,c := 0;range i < len(s); i++ {
switch s[i]c {
case '[':
open++
Line 1,672 ⟶ 4,055:
fmt.Println("not ok")
return
 
}
open--
Line 1,693 ⟶ 4,075:
}
testBalanced("()")
}</langsyntaxhighlight>
Output:
<pre>
Line 1,711 ⟶ 4,093:
=={{header|Groovy}}==
Generate Arbitrary String of Bracket Pairs:
<langsyntaxhighlight lang="groovy">def random = new Random()
 
def factorial = { (it > 1) ? (2..it).inject(1) { i, j -> i*j } : 1 }
Line 1,731 ⟶ 4,113:
def p = random.nextInt(factorial(n*2))
makePermutation(base, p)
}</langsyntaxhighlight>
 
Check Balance of Bracket String:
<langsyntaxhighlight lang="groovy">boolean balancedBrackets(String brackets, int depth=0) {
if (brackets == null || brackets.empty) return depth == 0
switch (brackets[0]) {
Line 1,744 ⟶ 4,126:
return brackets.size() == 1 ? depth == 0 : balancedBrackets(brackets[1..-1], depth)
}
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">Set brackets = []
(0..100).each {
(0..8).each { r ->
Line 1,759 ⟶ 4,141:
def bal = balancedBrackets(it) ? "balanced: " : "unbalanced: "
println "${bal} ${it}"
}</langsyntaxhighlight>
 
Output:
Line 1,957 ⟶ 4,339:
 
=={{header|Haskell}}==
The simplest solution exploits the idea of stack-based automaton, which could be implemented by a fold.
<lang haskell>
<syntaxhighlight lang="haskell">
isMatching :: String -> Bool
isMatching = null . foldl aut []
where
aut ('[':s) ']' = s
-- aut ('{':s) '}' = s -- automaton could be extended
aut s x = x:s
</syntaxhighlight>
 
This generates an infinite stream of correct balanced brackets expressions:
 
<syntaxhighlight lang="haskell">brackets = filter isMatching
$ [1.. ] >>= (`replicateM` "[]{}") </syntaxhighlight>
<pre>λ> take 10 brackets
["[]","{}","[[]]","[][]","[]{}","[{}]","{[]}","{{}}","{}[]","{}{}"]</pre>
 
In case the index of unmatched opening bracket is need to be found, following solution is suitable.
 
<syntaxhighlight lang="haskell">
import Control.Monad
import System.Random
Line 1,968 ⟶ 4,369:
isBalanced :: String -> Maybe Int
isBalanced = bal (-1) 0
where
where bal :: Int -> Int -> String -> Maybe Int
bal _ 0 :: Int -> Int -> []String -> =Maybe NothingInt
bal i _ 0 [] = Just iNothing
bal i (-1) _ [] = Just i
bal i n ('[':bs-1) _ = balJust (i+1) (n+1) bs
bal i n ('][':bs) = bal (i + 1) (n- + 1) bs
bal i n (']':bs) = bal (i + 1) (n - 1) bs
 
-- Print a string, indicating whether it contains balanced brackets. If not,
Line 1,979 ⟶ 4,381:
check :: String -> IO ()
check s = maybe (good s) (bad s) (isBalanced s)
where
where good s = printf "Good \"%s\"\n" s
bad s ngood = printf "Bad Good \"%s\"\n%*s^\n" s (n+6) " "
bad s n = printf "Bad \"%s\"\n%*s^\n" s (n + 6) " "
 
main :: IO ()
Line 1,986 ⟶ 4,389:
let bs = cycle "[]"
rs <- replicateM 10 newStdGen
zipWithM_ (\n r -> check $ shuffle (take n bs) r) [0,2 ..] rs</syntaxhighlight>
</lang>
We put our list shuffling function in a separate module. For efficiency we use ''mutable'' vectors, although for the short lists in our example it doesn't really matter.
<langsyntaxhighlight lang="haskell">module VShuffle
( shuffle
module VShuffle (shuffle) where
) where
 
import Data.List (mapAccumL)
Line 1,999 ⟶ 4,402:
 
-- Generate a list of array index pairs, each corresponding to a swap.
pairs
pairs :: (Enum a, Random a, RandomGen g) => a -> a -> g -> [(a, a)]
:: (Enum a, Random a, RandomGen g)
pairs l u r = snd $ mapAccumL step r [l..pred u]
=> a -> a -> g -> [(a, a)]
where step r i = let (j, r') = randomR (i, u) r in (r', (i, j))
pairs l u r = snd $ mapAccumL step r [l .. pred u]
where
step r i =
let (j, r') = randomR (i, u) r
in (r', (i, j))
 
-- Return a random permutation of the list. We use the algorithm described in
-- http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm.
shuffle
shuffle :: (RandomGen g) => [a] -> g -> [a]
:: (RandomGen g)
shuffle xs r = V.toList . runST $ do
=> [a] -> g -> [a]
v <- V.unsafeThaw $ V.fromList xs
shuffle xs r =
mapM_ (uncurry $ M.swap v) $ pairs 0 (M.length v - 1) r
V.toList . runST $
V.unsafeFreeze v
do v <- V.unsafeThaw $ V.fromList xs
</lang>
mapM_ (uncurry $ M.swap v) $ pairs 0 (M.length v - 1) r
V.unsafeFreeze v</syntaxhighlight>
Here's some sample output.
<pre>
Line 2,030 ⟶ 4,440:
Bad "]]][[[][][][[][]]["
^
</pre>
 
 
and '''scanl''' also yields a simple fit when we want the index of the tipping point:
 
<syntaxhighlight lang="haskell">import Control.Applicative ((<|>))
import Data.List (findIndex, replicate, scanl)
import Data.List.Split (chunksOf)
import System.Random
 
-------------------- BALANCED BRACKETS -------------------
 
nesting :: String -> [Int]
nesting = tail . scanl (flip level) 0
where
level '[' = succ
level ']' = pred
level _ = id
 
bracketProblemIndex :: String -> Maybe Int
bracketProblemIndex s =
findIndex (< 0) depths <|> unClosed
where
depths = nesting s
unClosed
| 0 /= last depths = Just $ pred (length s)
| otherwise = Nothing
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = do
let g = mkStdGen 137
mapM_ (putStrLn . showProblem) $
chunksOf
6
(bracket <$> take 60 (randomRs (0, 1) g))
 
showProblem s =
case bracketProblemIndex s of
Just i -> s <> ": Unmatched\n" <> replicate i ' ' <> "^"
_ -> s <> ": OK\n"
 
bracket :: Int -> Char
bracket 0 = '['
bracket _ = ']'</syntaxhighlight>
{{Out}}
<pre>]][]][: Unmatched
^
][[][[: Unmatched
^
][][[[: Unmatched
^
]][][]: Unmatched
^
[[][]]: OK
 
]]][]]: Unmatched
^
[][][[: Unmatched
^
[]]]]]: Unmatched
^
][[][[: Unmatched
^
[][]][: Unmatched
^
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main(arglist)
every s := genbs(!arglist) do
write(image(s), if isbalanced(s) then " is balanced." else " is unbalanced")
Line 2,047 ⟶ 4,525:
every !s := ?s # shuffle
return s
end</langsyntaxhighlight>
 
Output:<pre>
Line 2,065 ⟶ 4,543:
 
=={{header|J}}==
'''Solution''': <langsyntaxhighlight lang="j">bracketDepth =: '[]' -&(+/\)/@:(=/) ]
checkBalanced =: _1 -.@e. bracketDepth
genBracketPairs =: (?~@# { ])@#"0 1&'[]' NB. bracket pairs in arbitrary order</langsyntaxhighlight>
'''Examples''':<langsyntaxhighlight lang="j"> (,& ' ' , ('bad';'OK') {::~ checkBalanced)"1 genBracketPairs i. 10
OK
][ bad
Line 2,078 ⟶ 4,556:
[[]][[][][]][] OK
]]]][[][][[[[]][ bad
[]]][][][[[[]][[]] bad</langsyntaxhighlight>
'''Comments''': This task highlights the versatility and usefulness of J's scanning modifiers, <code>/</code> and <code>\</code>.
 
Line 2,085 ⟶ 4,563:
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">public class BracketsBalancedBrackets {
 
public static boolean checkBrackets(String str){
public static boolean hasBalancedBrackets(String str) {
int mismatchedBrackets = 0;
for(charint ch:str.toCharArray()){brackets = 0;
for if(char ch ==: '['str.toCharArray()) {
if (ch == '[') mismatchedBrackets++;{
}else if(ch == ']'){ brackets++;
} else if (ch mismatchedBrackets--;== ']') {
}else{ brackets--;
} else return false; //non-bracket chars{
return false; // non-bracket chars
}
if (mismatchedBracketsbrackets < 0) { //close closing bracket before openopening bracket
return false;
}
}
return mismatchedBracketsbrackets == 0;
}
 
public static String generategenerateBalancedBrackets(int n) {
if(assert n % 2 == 1){0; // if n is odd we can't match brackets
char[] ans = new return nullchar[n];
}
String ans = "";
int openBracketsLeft = n / 2;
int unclosed = 0;
whilefor (ans.length()int i = 0; i < n; i++) {
if (Math.random() >= 0.5 && openBracketsLeft > 0 || unclosed == 0) {
ans[i] += '[';
openBracketsLeft--;
unclosed++;
} else {
ans[i] += ']';
unclosed--;
}
}
return String.valueOf(ans);
}
 
public static void main(String[] args) {
String[]for tests(int i = {"",0; "[]",i "][",<= "[][]",16; "][][",i "[[][]]",+= "[]][[]"};2) {
for(int i = 0; iString brackets <= 16; generateBalancedBrackets(i+=2){;
StringSystem.out.println(brackets + ": bracks" =+ generatehasBalancedBrackets(ibrackets));
System.out.println(bracks + ": " + checkBrackets(bracks));
}
 
for(String test:[] tests) = {"", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"};
for (String System.out.println(test + ": "tests) + checkBrackets(test));{
System.out.println(test + ": " + hasBalancedBrackets(test));
}
}
}</langsyntaxhighlight>
Sample output (generate uses random numbers, so it should not be the same every time):
<pre>: true
Line 2,152 ⟶ 4,629:
[[][]]: true
[]][[]: false</pre>
 
=== Extended ===
<syntaxhighlight lang="java">import java.util.ArrayDeque;
import java.util.Deque;
 
public class BalancedBrackets {
public static boolean areSquareBracketsBalanced(String expr) {
return isBalanced(expr, "", "", "[", "]", false);
}
public static boolean areBracketsBalanced(String expr) {
return isBalanced(expr, "", "", "{([", "})]", false);
}
public static boolean areStringAndBracketsBalanced(String expr) {
return isBalanced(expr, "'\"", "\\\\", "{([", "})]", true);
}
public static boolean isBalanced(String expr, String lit, String esc, String obr, String cbr, boolean other) {
boolean[] inLit = new boolean[lit.length()];
Deque<Character> stack = new ArrayDeque<Character>();
for (int i=0; i<expr.length(); i+=1) {
char c = get(expr, i);
int x;
if ((x = indexOf(inLit, true)) != -1) {
if (c == get(lit, x) && get(expr, i-1) != get(esc, x)) inLit[x] = false;
}
else if ((x = indexOf(lit, c)) != -1)
inLit[x] = true;
else if ((x = indexOf(obr, c)) != -1)
stack.push(get(cbr, x));
else if (indexOf(cbr, c) != -1) {
if (stack.isEmpty() || stack.pop() != c) return false;
}
else if (!other)
return false;
}
return stack.isEmpty();
}
static int indexOf(boolean[] a, boolean b) {
for (int i=0; i<a.length; i+=1) if (a[i] == b) return i;
return -1;
}
static int indexOf(String s, char c) {
return s.indexOf(c);
}
static char get(String s, int i) {
return s.charAt(i);
}
 
public static void main(String[] args) {
System.out.println("With areSquareBracketsBalanced:");
for (String s: new String[] {
"", "[]", "[][]", "[[][]]", "][", "][][", "[]][[]", "[", "]"
}
) {
System.out.printf("%s: %s\n", s, areSquareBracketsBalanced(s));
}
System.out.println("\nBut also with areStringAndBracketsBalanced:");
for (String s: new String[] {
"x[]", "[x]", "[]x", "([{}])", "([{}]()", "([{ '([{\\'([{' \"([{\\\"([{\" }])",
}
) {
System.out.printf("%s: %s\n", s, areStringAndBracketsBalanced(s));
}
}
}</syntaxhighlight>
{{out}}
<pre>With areSquareBracketsBalanced:
: true
[]: true
[][]: true
[[][]]: true
][: false
][][: false
[]][[]: false
[: false
]: false
 
But also with areStringAndBracketsBalanced:
x[]: true
[x]: true
[]x: true
([{}]): true
([{}](): false
([{ '([{\'([{' "([{\"([{" }]): true
</pre>
 
=={{header|JavaScript}}==
 
<lang javascript>
===ES5===
function createRandomBracketSequence(maxlen)
====Iterative====
 
<syntaxhighlight lang="javascript">function shuffle(str) {
var a = str.split(''), b, c = a.length, d
while (c) b = Math.random() * c-- | 0, d = a[c], a[c] = a[b], a[b] = d
return a.join('')
}
 
function isBalanced(str) {
var a = str, b
do { b = a, a = a.replace(/\[\]/g, '') } while (a != b)
return !a
}
 
var M = 20
while (M-- > 0) {
var N = Math.random() * 10 | 0, bs = shuffle('['.repeat(N) + ']'.repeat(N))
console.log('"' + bs + '" is ' + (isBalanced(bs) ? '' : 'un') + 'balanced')
}</syntaxhighlight>
 
Sample output:
 
<pre>"[]" is balanced
"]][[]][[[]" is unbalanced
"]][[[][][][]][" is unbalanced
"[][[[[][][[[]]]]]]" is balanced
"][" is unbalanced
"[[[]]]][[]" is unbalanced
"][[]" is unbalanced
"][[][]][[[]]" is unbalanced
"[[][]]][" is unbalanced
"[[[]]][[]]]][][[" is unbalanced
"[]][[]]][[[[][]]" is unbalanced
"[]" is balanced
"][]][[][" is unbalanced
"[[]][[][]]" is balanced
"[[]]" is balanced
"]][]][[]][[[" is unbalanced
"][]][][[" is unbalanced
"[[]]" is balanced
"][][" is unbalanced
"[[]]][][][[]][" is unbalanced</pre>
 
==== Another solution ====
{{works with|Node.js}}
<syntaxhighlight lang="javascript">
console.log("Supplied examples");
var tests = ["", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"];
for (var test of tests)
{
console.log("The varstring chars'" =+ {test '0' :+ '["' ,is '1'" : ']'+ };
(isStringBalanced(test) ? "" : "not ") + "OK.");
function getRandomInteger(to)
}
{
console.log();
return Math.floor(Math.random() * (to+1));
console.log("Random generated examples");
}
for (var example = 1; example <= 10; example++)
var n = getRandomInteger(maxlen);
{
var result = [];
for(var itest = 0;generate(Math.floor(Math.random() i* <10) n; i++ 1);
console.log("The string '" + test + "' is " +
{
(isStringBalanced(test) ? "" : "not ") + "OK.");
result.push(chars[getRandomInteger(1)]);
}
return result.join("");
}
 
function bracketsAreBalancedisStringBalanced(sstr)
{
var paired = 0;
var open = (arguments.length > 1) ? arguments[1] : '[';
for (var closei = (arguments.length0; >i 2)< ?str.length arguments[2]&& :paired ']'>= 0; i++)
var c = 0;
for(var i = 0; i < s.length; i++)
{
var chc = s.charAt(str[i)];
if ( chc == open '[')
{ paired++;
else if (c++; == ']')
} paired--;
}
else if ( ch == close )
return (paired == 0);
}
function generate(n)
{
var opensCount = 0, closesCount = 0;
// Choose at random until n of one type generated
var generated = "";
while (opensCount < n && closesCount < n)
{
switch (Math.floor(Math.random() * 2) + 1)
{
c--;case 1:
if ( c < 0 ) return falseopensCount++;
generated += "[";
break;
case 2:
closesCount++;
generated += "]";
break;
default:
break;
}
}
// Now pad with the remaining other brackets
return c == 0;
generated +=
}
opensCount == n ? "]".repeat(n - closesCount) : "[".repeat(n - opensCount);
return generated;
}
</syntaxhighlight>
{{out}}
<pre>
Supplied examples
The string '' is OK.
The string '[]' is OK.
The string '][' is not OK.
The string '[][]' is OK.
The string '][][' is not OK.
The string '[[][]]' is OK.
The string '[]][[]' is not OK.
 
Random generated examples
var c = 0;
The string ']]][[[[[]][[[]]]]][[' is not OK.
while ( c < 5 ) {
The string '[][]]][]][[]]][[[[' is not OK.
var seq = createRandomBracketSequence(8);
The string ']][][[[][][][]' is not OK.
alert(seq + ':\t' + bracketsAreBalanced(seq));
The string '][]][[][[[][]]][' is not OK.
c++;
The string '[]][[[][]]' is not OK.
}
The string ']]][[[[]]]][]]][[[[[' is not OK.
</lang>
The string ']]]][][]][[[[[' is not OK.
Output:
The string '[][[[[][]][]]]' is OK.
The string '][[]]][[[[]]][[[]]' is not OK.
The string '[]' is OK.
</pre>
 
===ES6===
====Functional====
With visual indication of where the balance fails:
<syntaxhighlight lang="javascript">(() => {
'use strict';
 
// ---------------- BALANCED BRACKETS ----------------
 
// firstUnbalancedBracket :: String -> String -> Maybe Int
const firstUnbalancedBracket = brackets =>
haystack => {
const [openBracket, closeBracket] = [...brackets];
const go = (xs, iDepth, iCharPosn) =>
// iDepth: initial nesting depth (0 = closed)
// iCharPosn: starting character position
0 < xs.length ? (() => {
const
h = xs[0],
tail = xs.slice(1),
iNext = iDepth + (
brackets.includes(h) ? (
openBracket === h ? (
1
) : -1
) : 0
);
return 0 > iNext ? (
Just(iCharPosn) // Unmatched closing bracket.
) : 0 < tail.length ? go(
tail, iNext, 1 + iCharPosn
) : 0 !== iNext ? (
Just(iCharPosn)
) : Nothing();
})() : 0 !== iDepth ? (
Just(iCharPosn)
) : Nothing();
return go([...haystack], 0, 0);
};
 
 
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () => {
const
intPairs = 6,
strPad = ' '.repeat(4 + (2 * intPairs));
console.log(
enumFromTo(0)(intPairs)
.map(pairCount => {
const
stringLength = 2 * pairCount,
strSample = randomBrackets(stringLength);
return "'" + strSample + "'" +
strPad.slice(2 + stringLength) + maybe('OK')(
iUnMatched => 'problem\n' +
' '.repeat(1 + iUnMatched) + '^'
)(
firstUnbalancedBracket('[]')(strSample)
);
}).join('\n')
);
};
 
 
// Int -> String
const randomBrackets = n =>
enumFromTo(1)(n)
.map(() => Math.random() < 0.5 ? (
'['
) : ']').join('');
 
 
// --------------------- GENERIC ---------------------
 
// Just :: a -> Maybe a
const Just = x => ({
type: 'Maybe',
Nothing: false,
Just: x
});
 
 
// Nothing :: Maybe a
const Nothing = () => ({
type: 'Maybe',
Nothing: true,
});
 
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => !isNaN(m) ? (
Array.from({
length: 1 + n - m
}, (_, i) => m + i)
) : enumFromTo_(m)(n);
 
 
// maybe :: b -> (a -> b) -> Maybe a -> b
const maybe = v =>
// Default value (v) if m is Nothing, or f(m.Just)
f => m => m.Nothing ? (
v
) : f(m.Just);
 
// ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>'' OK
'[]' OK
'[][[' problem
^
'[]]][[' problem
^
'[][[][[[' problem
^
'[][[[]][]]' OK
']]][[][][][]' problem
^</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq.'''
 
In this entry, two solutions are given: the first uses
an external source of entropy and jq's `until` control structure;
the second uses a low-entropy PRNG based on jq's `now`,
and `gsub/2.`
===High entropy solution using `until`===
<syntaxhighlight lang=bash>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr '
 
# Output: a PRN in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def prb: 2 | prn == 0;
 
def balanced:
{array: explode, parity: 0}
| until( .result | type == "boolean";
if .array == [] then .result = (.parity == 0)
else .parity += (.array[0] | if . == 91 then 1 else -1 end)
| if .parity < 0 then .result = false
else .array |= .[1:]
end
end ).result ;
 
def task($n):
if $n%2 == 1 then null
else [ (range(0; $n) | if prb then "[" else "]" end) // ""]
| add
| "\(.): \(balanced)"
end;
 
task(0),
task(2),
(range(0;10) | task(4))
</syntaxhighlight>
{{output}}
<pre>
: true
[[]: false
][][[[][]: false
]][[[: false
[]][[]]: truefalse
[[]]: true
]][[: false
][[[: false
[[[[: false
[][[: false
][[[: false
[]][: false
</pre>
===Low entropy solution with `gsub`===
<syntaxhighlight lang=jq>
def prb: (now|tostring[-1:] | tonumber) % 2 == 0;
 
def balanced:
if length==0 then true
elif .[:1] == "]" then false
else test("[[][]]") and (gsub("[[][]]"; "") | balanced)
end;
 
def task($n):
Recursively remove occurrences of '[]':
if $n%2 == 1 then null
<lang javascript>
else
function checkBalance(i) {
while(reduce range(i.length0; %$n) 2as ==$i 0) {("";
. + j(if =prb i.replace('{}',''then "[" else "]" end) ));
| "\(.): if \(j == ibalanced)"
end;
break;
i = j;
}
return (i?false:true);
}
 
task(0),
var g = 10;
task(2),
while (g--) {
(range(0;10) | task(4))
var N = 10 - Math.floor(g/2), n=N, o='';
</syntaxhighlight>
while (n || N) {
{{output}}
if (N == 0 || n == 0) {
Similar to above.
o+=Array(++N).join('}') + Array(++n).join('{');
break;
}
if (Math.round(Math.random()) == 1) {
o+='}';
N--;
}
else {
o+='{';
n--;
}
}
alert(o+": "+checkBalance(o));
}
</lang>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Printf
<lang Julia>function balanced(str)
 
i = 0
function balancedbrackets(str::AbstractString)
for c in str
i = 0
if c == '[' i +=1 elseif c == ']' i -=1 end
iffor ic <in 0 return false endstr
if c == '[' i += 1 elseif c == ']' i -=1 end
end
i == 0 ? true : if i < 0 return false end
end
return i == 0
end
 
brackets(n::Integer) = CharStringjoin(shuffle([collect(Char, "[]" ^ n)...]))
 
printfor (test, pass) in map(x -> (x, balancedbalancedbrackets(x)), [collect(brackets(i) for i = 0:8]))
@printf("%22s%10s\n", test, pass ? "pass" : "fail")
</lang>
end</syntaxhighlight>
 
{{out}}
<pre> pass
<pre>("",true)
][ fail
("][",false)
[][] pass
("][[]",false)
][[]][ fail
("[[][]]",true)
[[[]]][] pass
("]][[[]][",false)
("[[ ]]]][[][]",false)[[ fail
("[[ ]]][[]][]][[[]",true) fail
(" ]][]][][[[][][[]",false) fail
("[[[[ []][]]]][[[]][[[]",false) fail</pre>
 
'''One-line version''':
<syntaxhighlight lang="julia">balancedbrackets(str::AbstractString) = foldl((x, y) -> x < 0 ? -1 : x + y, collect((x == '[') - (x == ']') for x in str), init = 0) == 0</syntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">
gen_brackets:{"[]"@x _draw 2}
check:{r:(-1;1)@"["=x; *(0=+/cs<'0)&(0=-1#cs:+\r)}
{(x;check x)}' gen_brackets' 2*1+!10
(("[[";0)
("[][]";1)
("][][]]";0)
("[[][[][]";0)
("][]][[[[[[";0)
("]]][[]][]]][";0)
("[[[]][[[][[[][";0)
("[[]][[[]][]][][]";1)
("][[][[]]][[]]]][][";0)
("]][[[[]]]][][][[]]]]";0))
</syntaxhighlight>
 
=={{header|Klingphix}}==
<syntaxhighlight lang="text">"[[]][]]"
 
%acc 0 !acc
%flag false !flag
len [
get tochar dup
"[" == [$acc 1 + !acc] if
"]" == [$acc 1 - !acc] if
$acc 0 < [true !flag] if
] for
print
 
$acc 0 # $flag or ( [" is NOT ok"] [" is OK"] ) if
print
 
" " input</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="scala">import java.util.Random
 
fun isBalanced(s: String): Boolean {
if (s.isEmpty()) return true
var countLeft = 0 // number of left brackets so far unmatched
for (c in s) {
if (c == '[') countLeft++
else if (countLeft > 0) countLeft--
else return false
}
return countLeft == 0
}
 
fun main(args: Array<String>) {
println("Checking examples in task description:")
val brackets = arrayOf("", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]")
for (b in brackets) {
print(if (b != "") b else "(empty)")
println("\t " + if (isBalanced(b)) "OK" else "NOT OK")
}
println()
 
println("Checking 7 random strings of brackets of length 8:")
val r = Random()
(1..7).forEach {
var s = ""
for (j in 1..8) {
s += if (r.nextInt(2) == 0) '[' else ']'
}
println("$s " + if (isBalanced(s)) "OK" else "NOT OK")
}
}</syntaxhighlight>
Sample output (last 7 lines random) :
{{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
[[[[[]]] NOT OK
[[[[[]]] NOT OK
[[[]]][] OK
]]]][[][ NOT OK
]][]][][ NOT OK
</pre>
 
=={{header|L++}}==
<syntaxhighlight lang="lisp">(include "string")
(defn bool balanced (std::string s)
(def bal 0)
(foreach c s
(if (== c #\[) (++ bal)
(if (== c #\]) (-- bal)))
(if (< bal 0) (return false)))
(return (== bal 0)))
(main
(decl std::string (at tests) |{"", "[]", "[][]", "[[][]]", "][", "][][", "[]][[]"}|)
(pr std::boolalpha)
(foreach x tests
(prn x "\t" (balanced x))))</syntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define randomparens(num::integer,open::string='[',close::string=']') => {
local(out) = array
 
Line 2,290 ⟶ 5,216:
with i in 1 to 10
let input = randomparens(#i)
select #input + ' = ' + validateparens(#input)</langsyntaxhighlight>
 
{{out}}
Line 2,304 ⟶ 5,230:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
print "Supplied examples"
for i =1 to 7
Line 2,367 ⟶ 5,293:
 
end
</syntaxhighlight>
</lang>
Supplied examples
The string '' is OK.
Line 2,388 ⟶ 5,314:
The string '[[]]][][[][]' is not OK. Unbalanced.
The string '][[][[][][]]][[]' is not OK. Unbalanced.
 
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
function isBalanced(s)
--Lua pattern matching has a 'balanced' pattern that matches sets of balanced characters.
Line 2,412 ⟶ 5,337:
print(RS)
print(isBalanced(RS))
</syntaxhighlight>
</lang>
 
=={{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<1 then max=1
def putbracket()=mid$("[[[[]]",random(1,6),1)
dim a(random(1, max)) 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 for
next
=level=0
}
string k
boolean m
do
k=randomBrackets(10)
m=check(k)
print k;@(12);": ";if$(m->"OK", "NOT OK")
until m
}
Balanced_brackets
</syntaxhighlight>
{{out}}
<pre>
[[[] : NOT OK
[[][]] : OK
</pre>
 
=={{header|Maple}}==
This functionality is provided by Maple.
<syntaxhighlight lang="maple">
<lang Maple>
> use StringTools in
> IsBalanced( "", "[", "]" );
Line 2,448 ⟶ 5,409:
 
false
</syntaxhighlight>
</lang>
Furthermore, Maple can check whether multiple fences are balanced in the same string.
<syntaxhighlight lang="maple">
<lang Maple>
> StringTools:-IsBalanced( "[()()]", "[(", "])" );
true
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">(* Generate open/close events. *)
(* Generate open/close events. *)
gen[n_] := RandomSample[Table[{1, -1}, {n}] // Flatten]
 
Line 2,469 ⟶ 5,429:
Print[str <> If[match[lst, 0],
" is balanced.",
" is not balanced."]])</syntaxhighlight>
</lang>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight lang="matlab">function x = isbb(s)
t = cumsum((s=='[') - (s==']'));
x = all(t>=0) && (t(end)==0);
end;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,496 ⟶ 5,455:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">brack(s) := block(
[n: slength(s), r: 0, c],
catch(
Line 2,526 ⟶ 5,485:
 
brack("[[[]][]]]");
false</langsyntaxhighlight>
 
=={{header|Mercury}}==
<syntaxhighlight lang="mercury">
:- module balancedbrackets.
:- interface.
 
:- import_module io.
 
:- pred main(io::di, io::uo) is det.
 
 
:- import_module list, random, char.
 
:- pred brackets(int::in,list(char)::out,supply::mdi,supply::muo) is det.
 
:- pred imbalance(list(char)::in,int::out) is semidet.
:- pred balanced(list(char)::in) is semidet.
 
:- implementation.
 
:- import_module int.
 
imbalance([],0).
imbalance(['['|T],N) :- imbalance(T,N+1).
imbalance([']'|T],N) :- N > 0, imbalance(T,N-1).
 
balanced(S) :- imbalance(S,0).
 
brackets(N,S,!RS) :-
(
N < 1 -> S is []
; random(0,2,R,!RS),
( R is 0 -> S is ['['|T], brackets(N-1,T,!RS)
; S is [']'|T], brackets(N-1,T,!RS))).
 
main(!IO) :-
random.init(0,RS),
brackets(4,S,RS,_),
print(S,!IO),
(
balanced(S) -> print(" is balanced\n",!IO)
; print(" is unbalanced\n", !IO)
).
</syntaxhighlight>
 
=={{header|MiniScript}}==
 
We start by defining a function:
 
<syntaxhighlight 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 level == 0
end function</syntaxhighlight>
 
We can evaluate the example strings with this code:
 
<syntaxhighlight 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</syntaxhighlight>
 
{{out}}
<pre>"" is OK
"[]" is OK
"[][]" is OK
"[[][]]" is OK
"][" NOT OK
"][][" NOT OK
"[]][[]" NOT OK
"[[[]" NOT OK</pre>
 
=={{header|Modula-2}}==
{{works with|TopSpeed (JPI) Modula-2 under DOSBox-X}}
An interesting point is how to ensure that all strings of N left plus N right brackets are equally likely. The program below shows one way of doing this.
<syntaxhighlight lang="modula2">
MODULE Brackets;
IMPORT IO, Lib;
 
CONST MaxN = 4;
 
PROCEDURE DoTest( N : CARDINAL);
VAR
brStr : ARRAY [0..2*MaxN] OF CHAR; (* string of brackets *)
verdict : ARRAY [0..2] OF CHAR;
br : CHAR;
k, nL, nR, randNum : CARDINAL;
count : INTEGER;
BEGIN
k := 0; (* index into brStr *)
nL := N; nR := N; (* number of left/right brackets remaining *)
WHILE (nL > 0) AND (nR > 0) DO
randNum := Lib.RANDOM( nL + nR);
IF (randNum < nL) THEN brStr[k] := '['; DEC(nL);
ELSE brStr[k] := ']'; DEC(nR); END;
INC(k);
END;
(* Here when only one kind of bracket is possible *)
IF (nL = 0) THEN br := ']' ELSE br := '['; END;
WHILE (k < 2*N) DO brStr[k] := br; INC(k); END;
brStr[k] := 0C; (* null to mark end of string *)
 
(* Test for balance *)
count := 0;
k := 0;
REPEAT
IF brStr[k] = '[' THEN INC( count) ELSE DEC( count) END;
INC( k);
UNTIL (count < 0) OR (k = 2*N);
IF (count < 0) THEN verdict := 'no' ELSE verdict := 'yes' END;
IO.WrStr( brStr); IO.WrStr(' '); IO.WrStr( verdict);
IO.WrLn;
END DoTest;
 
(* Main routine *)
VAR
j : CARDINAL;
BEGIN
Lib.RANDOMIZE;
FOR j := 1 TO 10 DO
DoTest( Lib.RANDOM(MaxN) + 1);
END;
END Brackets.
</syntaxhighlight>
{{out}}
<pre>
[]][[]][ no
[[[]]] yes
][ no
][]][[ no
[][[]] yes
][[] no
[][] yes
][][]][[ no
[] yes
[]][[][] no
</pre>
 
=={{header|Nanoquery}}==
{{trans|Python}}
<syntaxhighlight lang="nanoquery">import Nanoquery.Util
 
def gen(N)
txt = {"[", "]"} * N
txt = new(Random).shuffle(txt)
return "".join("", txt)
end
 
def balanced(txt)
braced = 0
for ch in txt
if ch = "["
braced += 1
else if ch = "]"
braced -= 1
end
if braced < 0
return false
end
end
return braced = 0
end
 
// unlike Python, the range function is inclusive in Nanoquery
for N in range(1, 10)
txt = gen(N)
if balanced(txt)
println format("%-22s is balanced", txt)
else
println format("%-22s is not balanced", txt)
end
end</syntaxhighlight>
{{out}}
<pre> is balanced
[] is balanced
[][] is balanced
[]][][ is not balanced
[]][[[]] is not balanced
[][[[[]]]] is balanced
][[]][][]][[ is not balanced
][]][[][]][[[] is not balanced
[[]][[[]][]][]][ is not balanced
[[][[]][][]]][[[]] is not balanced
[[][][[[][]][[]]][]] is balanced</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
from random import random, randomize, shuffle
from strutils import repeat
 
randomize()
 
proc gen(n: int): string =
result = "[]".repeat(n)
shuffle(result)
 
proc balanced(txt: string): bool =
var b = 0
for c in txt:
case c
of '[':
inc(b)
of ']':
dec(b)
if b < 0: return false
else: discard
b == 0
 
for n in 0..9:
let s = gen(n)
echo "'", s, "' is ", (if balanced(s): "balanced" else: "not balanced")</syntaxhighlight>
Output:
<pre>'' is balanced
'][' is not balanced
'][[]' is not balanced
'[][[]]' is balanced
'[[]][][]' is balanced
']][][[[][]' is not balanced
'][]][][][[][' is not balanced
'[[[[[[]]]][]]]' is balanced
'][[][]]]][[[[][]' is not balanced
'][][][][][[][[]]][' is not balanced</pre>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
def gen_brackets [n: int] { 1..$in | each {["[" "]"]} | flatten | shuffle | str join }
 
def check_brackets [] {
split chars | reduce --fold 0 {|x, d|
if ($d < 0) {-1} else {
$d + (if ($x == "[") {1} else {-1})
}
} | $in > -1
}
 
 
1..10 | each {gen_brackets $in | {brackets: $in, valid: ($in | check_brackets)}} | print
</syntaxhighlight>
{{out}}
<pre>
╭───┬──────────────────────┬───────╮
│ # │ brackets │ valid │
├───┼──────────────────────┼───────┤
│ 0 │ ][ │ false │
│ 1 │ []][ │ false │
│ 2 │ [[][]] │ true │
│ 3 │ [[[][]]] │ true │
│ 4 │ ]]][[[[][] │ false │
│ 5 │ [][][][[[]]] │ true │
│ 6 │ ]]]]][]][[[[[[ │ false │
│ 7 │ ][[][]]]][[][[[] │ false │
│ 8 │ []]]][][][[][]][[[ │ false │
│ 9 │ ][][][[[[[]]]][[]][] │ false │
╰───┴──────────────────────┴───────╯
</pre>
 
=={{header|Oberon-2}}==
{{works with|oo2c version 2}}
<syntaxhighlight lang="oberon2">
MODULE BalancedBrackets;
IMPORT
Object,
Object:Boxed,
ADT:LinkedList,
ADT:Storable,
IO,
Out := NPCT:Console;
 
TYPE
(* CHAR is not boxed in the standard lib *)
(* so make a boxed char *)
Character* = POINTER TO CharacterDesc;
CharacterDesc* = RECORD
(Boxed.ObjectDesc)
c: CHAR;
END;
 
(* Method for a boxed char *)
PROCEDURE (c: Character) INIT*(x: CHAR);
BEGIN
c.c := x;
END INIT;
 
PROCEDURE NewCharacter*(c: CHAR): Character;
VAR
x: Character;
BEGIN
NEW(x);x.INIT(c);RETURN x
END NewCharacter;
 
PROCEDURE (c: Character) ToString*(): STRING;
BEGIN
RETURN Object.NewLatin1Char(c.c);
END ToString;
 
PROCEDURE (c: Character) Load*(r: Storable.Reader) RAISES IO.Error;
BEGIN
r.ReadChar(c.c);
END Load;
 
PROCEDURE (c: Character) Store*(w: Storable.Writer) RAISES IO.Error;
BEGIN
w.WriteChar(c.c);
END Store;
 
PROCEDURE (c: Character) Cmp*(o: Object.Object): LONGINT;
BEGIN
IF c.c < o(Character).c THEN RETURN -1
ELSIF c.c = o(Character).c THEN RETURN 0
ELSE RETURN 1
END
END Cmp;
(* end of methods for a boxed char *)
 
PROCEDURE CheckBalance(str: STRING): BOOLEAN;
VAR
s: LinkedList.LinkedList(Character);
chars: Object.CharsLatin1;
n, x: Boxed.Object;
i,len: LONGINT;
BEGIN
i := 0;
chars := str(Object.String8).CharsLatin1();
len := str.length;
s := NEW(LinkedList.LinkedList(Character));
WHILE (i < len) & (chars[i] # 0X) DO
IF s.IsEmpty() THEN
s.Append(NewCharacter(chars[i])) (* Push character *)
ELSE
n := s.GetLast(); (* top character *)
WITH
n: Character DO
IF (chars[i] = ']') & (n.c = '[') THEN
x := s.RemoveLast(); (* Pop character *)
x := NIL
ELSE
s.Append(NewCharacter(chars[i]))
END
ELSE RETURN FALSE
END (* WITH *)
END;
INC(i)
END;
RETURN s.IsEmpty()
END CheckBalance;
 
PROCEDURE Do;
VAR
str: STRING;
BEGIN
str := "[]";Out.String(str + ":> "); Out.Bool(CheckBalance(str));Out.Ln;
str := "[][]";Out.String(str + ":> ");Out.Bool(CheckBalance(str));Out.Ln;
str := "[[][]]";Out.String(str + ":> ");Out.Bool(CheckBalance(str));Out.Ln;
str := "][";Out.String(str + ":> ");Out.Bool(CheckBalance(str));Out.Ln;
str := "][][";Out.String(str + ":> ");Out.Bool(CheckBalance(str));Out.Ln;
str := "[]][[]";Out.String(str + ":> ");Out.Bool(CheckBalance(str));Out.Ln;
END Do;
 
BEGIN
Do
END BalancedBrackets.
</syntaxhighlight>
{{out}}
<pre>
[]:> TRUE
[][]:> TRUE
[[][]]:> TRUE
][:> FALSE
][][:> FALSE
[]][[]:> FALSE
</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
bundle Default {
class Balanced {
Line 2,562 ⟶ 5,908:
}
}
</syntaxhighlight>
</lang>
<pre>
: true
Line 2,575 ⟶ 5,921:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let generate_brackets n =
let rec aux i acc =
if i <= 0 then acc else
Line 2,599 ⟶ 5,945:
List.iter print_char brk;
Printf.printf " %B\n" (is_balanced brk);
;;</langsyntaxhighlight>
 
<pre>
Line 2,606 ⟶ 5,952:
$ ocaml balanced_brackets.ml 3
[[]][] true
</pre>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">String method: isBalanced
| c |
0 self forEach: c [
c '[' == ifTrue: [ 1+ continue ]
c ']' <> ifTrue: [ continue ]
1- dup 0 < ifTrue: [ drop false return ]
]
0 == ;
: genBrackets(n)
"" #[ "[" "]" 2 rand 2 == ifTrue: [ swap ] rot + swap + ] times(n) ;</syntaxhighlight>
 
{{out}}
<pre>
#[ genBrackets(5) dup print " -->" print isBalanced println ] times(10)
[[][[]][]] -->-1
][][][][][ -->0
][[][][]][ -->0
][[]][[]][ -->0
[][][][][] -->-1
]]][][][[[ -->0
[[[[[]]]]] -->-1
[[[[[]]]]] -->-1
][][][][][ -->0
]]][][][[[ -->0
</pre>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
tests = .array~of("", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]")
 
Line 2,657 ⟶ 6,032:
end
return answer~string
</syntaxhighlight>
</lang>
Sample output (uses randomly generated groupings, so it should be different on each run):
<pre>
Line 2,678 ⟶ 6,053:
 
=={{header|OxygenBasic}}==
<langsyntaxhighlight lang="oxygenbasic">function CheckBrackets(string s) as bool
'=======================================
sys co, le=len s
Line 2,705 ⟶ 6,080:
print CheckBrackets "[][]"'1
print CheckBrackets "][" '0
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">balanced(s)={
my(n=0,v=Vecsmall(s));
for(i=1,#v,
Line 2,720 ⟶ 6,095:
};
rnd(n)=Strchr(vectorsmall(n,i,if(random(2),91,93)))
forstep(n=0,10,2,s=rnd(n);print(s"\t"if(balanced(s),"true","false")))</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 2,729 ⟶ 6,104:
Idiomatic solution, using a regex that performs subpattern recursion ''(works with Perl 5.10 and newer)'':
 
<langsyntaxhighlight Perllang="perl">sub generate {
my $n = shift;
my $str = '[' x $n;
Line 2,743 ⟶ 6,118:
my $input = generate($_);
print balanced($input) ? " ok:" : "bad:", " '$input'\n";
}</langsyntaxhighlight>
 
{{out}}
Line 2,757 ⟶ 6,132:
bad: ']][]]][[][][[][['
</pre>
 
If input strings are allowed to contain unrelated characters, this can be extended to:
 
<syntaxhighlight lang="perl">sub balanced {
shift =~ /^ ( [^\[\]]++ | \[ (?1)* \] )* $/x;
}</syntaxhighlight>
 
<code>Regexp::Common::balanced</code> can give such a regexp too (here via a subroutine call)
 
<syntaxhighlight lang="perl">use Regexp::Common 'RE_balanced';
sub balanced {
return shift =~ RE_balanced(-parens=>'[]')
}
</syntaxhighlight>
 
Alternative implementation, using straightforward depth counting:
 
<langsyntaxhighlight Perllang="perl">sub balanced {
my $depth = 0;
for (split //, shift) {
Line 2,767 ⟶ 6,156:
}
return !$depth
}</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
There's More Than One Way To Do It.
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
===Depth counter===
<span style="color: #008080;">function</span> <span style="color: #000000;">check_brackets</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<lang perl6>sub balanced($s) {
<span style="color: #004080;">integer</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
my $l = 0;
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for $s.comb {
<span style="color: #008080;">switch</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
when "]" {
<span style="color: #008080;">case</span> <span style="color: #008000;">'['</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
--$l;
<span style="color: #008080;">case</span> <span style="color: #008000;">']'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
return False if $l < 0;
<span style="color: #008080;">if</span> <span style="color: #000000;">level</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
}
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
when "[" {
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
++$l;
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">level</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"not ok"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ok"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"[]"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok</span><span style="color: #0000FF;">[</span><span style="color: #000000;">check_brackets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
ok
ok
[] ok
][ not ok
[][] ok
][][ not ok
[]][][ not ok
][][[] not ok
][[][]][ not ok
[][[][]] ok
[]]][[[]][ not ok
][]][[[]][ not ok
][[]]]][[][[ not ok
[][[]][[]][] ok
[]][][]]][[[[] not ok
[[][][]][][[]] ok
[[][]][][[]][[]] ok
][][[[[][]]][]][ not ok
[[[[]]][][[[][]]]] ok
[[[]]][[[[][]]][]] ok
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">"[[]][]"
0 var acc
 
len for
get dup
'[' == if acc 1 + var acc endif
']' == if acc 1 - var acc endif
acc 0 < if exitfor endif
endfor
 
print
acc 0 < if
" is NOT ok"
else
" is OK"
endif
print</syntaxhighlight>
 
=={{header|PHP}}==
The sample is given as unix shell script, you need to have ''php-cli'' (or what your package manager calls it) installed.
 
<syntaxhighlight lang="php">#!/usr/bin/php
<?php
 
# brackets generator
function bgenerate ($n) {
if ($n==0) return '';
$s = str_repeat('[', $n) . str_repeat(']', $n);
return str_shuffle($s);
}
 
function printbool($b) {return ($b) ? 'OK' : 'NOT OK';}
 
function isbalanced($s) {
$bal = 0;
for ($i=0; $i < strlen($s); $i++) {
$ch = substr($s, $i, 1);
if ($ch == '[') {
$bal++;
} else {
$bal--;
}
if ($bal < 0) return false;
}
return ($lbal == 0);
}
 
# test parameters are N (see spec)
my $n = prompt "Number of brackets";
$tests = array(0, 2,2,2, 3,3,3, 4,4,4,4);
my $s = (<[ ]> xx $n).pick(*).join;
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</lang>
 
foreach ($tests as $v) {
===FP oriented===
$s = bgenerate($v);
Here's a more idiomatic solution using a hyperoperator to compare all the characters to a backslash (which is between the brackets in ASCII), a triangle reduction to return the running sum, a <tt>given</tt> to make that list the topic, and then a topicalized junction and a topicalized subscript to test the criteria for balance.
printf("%s\t%s%s", $s, printbool(isbalanced($s)), PHP_EOL);
<lang perl6>sub balanced($s) {
.none < 0 and .[*-1] == 0
given [\+] '\\' «leg« $s.comb;
}
</syntaxhighlight>
Sample run:
<pre>
OK
[][] OK
[[]] OK
[]][ NOT OK
][][[] NOT OK
[][[]] OK
][[[]] NOT OK
]][[][][ NOT OK
[]][][[] NOT OK
][]]][[[ NOT OK
[[[][]]] OK
</pre>
 
=={{header|Picat}}==
my $n = prompt "Number of bracket pairs: ";
===Foreach loop===
my $s = <[ ]>.roll($n*2).join;
<syntaxhighlight lang="picat">go1 ?=>
say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"</lang>
tests(Tests),
===String munging===
member(Test,Tests),
Of course, a Perl 5 programmer might just remove as many inner balanced pairs as possible and then see what's left.
printf("%s: ", Test),
<lang perl6>sub balanced($_ is copy) {
( balanced_brackets(Test) ->
() while s:g/'[]'//;
$_ eq ''; println("OK")
;
}
println("NOT OK")
),
fail,
nl.
go1 => true.
 
% Check if a string of [] is balanced
my $n = prompt "Number of bracket pairs: ";
balanced_brackets(B) =>
my $s = <[ ]>.roll($n*2).join;
C = 0,
say "$s is", ' not' xx not balanced($s)), " well-balanced";</lang>
foreach(I in 1..B.length, C >= 0)
===Parsing with a grammar===
C:= C + cond(B[I] = '[', 1, -1)
<lang perl6>grammar BalBrack {
end,
token TOP { ^ <balanced>* $ };
C == 0.
token balanced { '[' <balanced>* ']' }
}
 
tests(["","[]", "[][]", "[[][]]", "][",
my $n = prompt "Number of bracket pairs: ";
"][][", "[]][[]", "[][][][][][][][][][]",
my $s = (<[ ]> xx $n).pick(*).join;
"[[[[[[[]]]]]]]", "[[[[[[[]]]]]]",
say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";</lang>
"[][[]][]","[[][]][]", "[][][[]][]"]).</syntaxhighlight>
 
{{out}}
<pre>: OK
[]: OK
[][]: OK
[[][]]: OK
][: NOT OK
][][: NOT OK
[]][[]: NOT OK
[][][][][][][][][][]: OK
[[[[[[[]]]]]]]: OK
[[[[[[[]]]]]]: NOT OK
[][[]][]: OK
[[][]][]: OK
[][][[]][]: OK</pre>
 
===DCG===
Here is an implementation using DCG (Definite Clause Grammars).
<syntaxhighlight lang="picat">go_dcg ?=>
tests(Tests),
foreach(Test in Tests)
printf("%s: ", Test),
if balanced(Test,[]) then
println("OK")
else
println("NOT OK")
end
end,
nl.
go_dcg => true.
 
balanced --> "".
balanced --> "[", balanced, "]", balanced.</syntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/simul.l") # For 'shuffle'
 
(de generateBrackets (N)
Line 2,837 ⟶ 6,356:
 
(for N 10
(prinl (if (checkBrackets (prin (generateBrackets N))) " OK" "not OK")) )</langsyntaxhighlight>
Output:
<pre>[] OK
Line 2,851 ⟶ 6,370:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process m or(!) s attributes source;
cb: Proc Options(main);
/* PL/I program to check for balanced brackets [] ********************
Line 2,912 ⟶ 6,431:
End;
 
End;</langsyntaxhighlight>
Output:
<pre> balanced ''
Line 2,940 ⟶ 6,459:
unbalanced '[]][]]][[]'
unbalanced '][[][[[[[]'</pre>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
function Get-BalanceStatus ( $String )
{
$Open = 0
ForEach ( $Character in [char[]]$String )
{
switch ( $Character )
{
"[" { $Open++ }
"]" { $Open-- }
default { $Open = -1 }
}
# If Open drops below zero (close before open or non-allowed character)
# Exit loop
If ( $Open -lt 0 ) { Break }
}
$Status = ( "NOT OK", "OK" )[( $Open -eq 0 )]
return $Status
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
# Test
$Strings = @( "" )
$Strings += 1..5 | ForEach { ( [char[]]("[]" * $_) | Get-Random -Count ( $_ * 2 ) ) -join "" }
ForEach ( $String in $Strings )
{
$String.PadRight( 12, " " ) + (Get-BalanceStatus $String)
}
</syntaxhighlight>
{{out}}
<pre>
OK
[] OK
]][[ NOT OK
]][][[ NOT OK
[[[][]]] OK
][[[]][][] NOT OK
</pre>
 
 
===PowerShell (Regex Version)===
<syntaxhighlight lang="powershell">
function Test-BalancedBracket
{
<#
.SYNOPSIS
Tests a string for balanced brackets.
.DESCRIPTION
Tests a string for balanced brackets. ("<>", "[]", "{}" or "()")
.EXAMPLE
Test-BalancedBracket -Bracket Brace -String '{abc(def[0]).xyz}'
Test a string for balanced braces.
.EXAMPLE
Test-BalancedBracket -Bracket Curly -String '{abc(def[0]).xyz}'
Test a string for balanced curly braces.
.EXAMPLE
Test-BalancedBracket -Bracket Curly -String ([System.IO.File]::ReadAllText('.\Foo.ps1'))
Test a file for balanced curly braces.
.LINK
http://go.microsoft.com/fwlink/?LinkId=133231
#>
[CmdletBinding()]
[OutputType([bool])]
Param
(
[Parameter(Mandatory=$true)]
[ValidateSet("Angle", "Brace", "Curly", "Paren")]
[string]
$Bracket,
 
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]
$String
)
 
$notFound = -1
 
$brackets = @{
Angle = @{Left="<"; Right=">"; Regex="^[^<>]*(?>(?>(?'pair'\<)[^<>]*)+(?>(?'-pair'\>)[^<>]*)+)+(?(pair)(?!))$"}
Brace = @{Left="["; Right="]"; Regex="^[^\[\]]*(?>(?>(?'pair'\[)[^\[\]]*)+(?>(?'-pair'\])[^\[\]]*)+)+(?(pair)(?!))$"}
Curly = @{Left="{"; Right="}"; Regex="^[^{}]*(?>(?>(?'pair'\{)[^{}]*)+(?>(?'-pair'\})[^{}]*)+)+(?(pair)(?!))$"}
Paren = @{Left="("; Right=")"; Regex="^[^()]*(?>(?>(?'pair'\()[^()]*)+(?>(?'-pair'\))[^()]*)+)+(?(pair)(?!))$"}
}
 
if ($String.IndexOf($brackets.$Bracket.Left) -eq $notFound -and
$String.IndexOf($brackets.$Bracket.Right) -eq $notFound -or $String -eq [String]::Empty)
{
return $true
}
 
$String -match $brackets.$Bracket.Regex
}
 
 
'', '[]', '][', '[][]', '][][', '[[][]]', '[]][[]' | ForEach-Object {
if ($_ -eq "") { $s = "(Empty)" } else { $s = $_ }
"{0}: {1}" -f $s.PadRight(8), "$(if (Test-BalancedBracket Brace $s) {'Is balanced.'} else {'Is not balanced.'})"
}
</syntaxhighlight>
{{Out}}
<pre>
(Empty) : Is balanced.
[] : Is balanced.
][ : Is not balanced.
[][] : Is balanced.
][][ : Is not balanced.
[[][]] : Is balanced.
[]][[] : Is not balanced.
</pre>
 
=={{header|Prolog}}==
DCG are very usefull for this kind of exercice !
<langsyntaxhighlight Prologlang="prolog">rosetta_brackets :-
test_brackets([]),
test_brackets(['[',']']),
Line 2,995 ⟶ 6,628:
bracket(0) --> ['['].
bracket(1) --> [']'].
</syntaxhighlight>
</lang>
Sample output :
<pre> ?- balanced_brackets.
Line 3,022 ⟶ 6,655:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.s Generate(N)
For i=1 To N
sample$+"[]"
Line 3,065 ⟶ 6,698:
PrintN(" is not balanced")
EndIf
Next</langsyntaxhighlight>
Output sample
<pre>
Line 3,076 ⟶ 6,709:
 
=={{header|Python}}==
===Procedural===
<lang python>>>> def gen(N):
<syntaxhighlight lang="python">>>> def gen(N):
... txt = ['[', ']'] * N
... random.shuffle( txt )
Line 3,102 ⟶ 6,736:
'[[]]]]][]][[[[' is not balanced
'[[[[]][]]][[][]]' is balanced
'][[][[]]][]]][[[[]' is not balanced</langsyntaxhighlight>
 
=== Functional ===
{{works with|Python|3.2}}
Rather than explicitly track the count, we can just write the per-element test and use stdlib functions to turn it into a whole-sequence test. It's straightforwardly declarative, and hard to get wrong, but whether it's actually easier to understand depends on how familiar the reader is with thinking in `itertools` style.
 
<syntaxhighlight lang="python">>>> from itertools import accumulate
>>> from random import shuffle
>>> def gen(n):
... txt = list('[]' * n)
... shuffle(txt)
... return ''.join(txt)
...
>>> def balanced(txt):
... brackets = ({'[': 1, ']': -1}.get(ch, 0) for ch in txt)
... return all(x>=0 for x in accumulate(brackets))
...
>>> for txt in (gen(N) for N in range(10)):
... print ("%-22r is%s balanced" % (txt, '' if balanced(txt) else ' not'))
...
'' is balanced
'][' is not balanced
'[]][' is not balanced
']][[[]' is not balanced
'][[][][]' is not balanced
'[[[][][]]]' is balanced
'][[[][][]][]' is not balanced
'][]][][[]][[][' is not balanced
'][[]]][][[]][[[]' is not balanced
'][[][[]]]][[[]][][' is not balanced</syntaxhighlight>
 
=== Array Programming ===
{{libheader|NumPy}}
The numpy library gives us a way to write just the elementwise tests and automatically turn them into whole-sequence tests, although it can be a bit clumsy to use for character rather than numeric operations. The simplicity of the final expression probably doesn't make up for all that extra clumsiness in this case.
 
<syntaxhighlight lang="python">>>> import numpy as np
>>> from random import shuffle
>>> def gen(n):
... txt = list('[]' * n)
... shuffle(txt)
... return ''.join(txt)
...
>>> m = np.array([{'[': 1, ']': -1}.get(chr(c), 0) for c in range(128)])
>>> def balanced(txt):
... a = np.array(txt, 'c').view(np.uint8)
... return np.all(m[a].cumsum() >= 0)
...
>>> for txt in (gen(N) for N in range(10)):
... print ("%-22r is%s balanced" % (txt, '' if balanced(txt) else ' not'))
...
'' is balanced
'][' is not balanced
'[[]]' is balanced
'[]][][' is not balanced
']][]][[[' is not balanced
'[[]][[][]]' is balanced
'[][[]][[]]][' is not balanced
'[][[[]][[]]][]' is balanced
'[[][][[]]][[[]]]' is balanced
'][]][][[]][]][][[[' is not balanced</syntaxhighlight>
 
=={{header|Qi}}==
 
<langsyntaxhighlight lang="qi">(define balanced-brackets-0
[] 0 -> true
[] _ -> false
Line 3,128 ⟶ 6,821:
(balanced-brackets "[]][[]")
 
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ char [ over of
swap
char ] swap of
join shuffle ] is bracket$ ( n --> $ )
 
[ 0 swap witheach
[ char [ =
iff 1 else -1 +
dup 0 < if
conclude ]
0 = ] is balanced ( $ --> b )
 
 
10 times
[ 20 i 2 * - times sp
i bracket$ dup echo$
say " is "
balanced not if
[ say "un" ]
say "balanced."
cr ]</syntaxhighlight>
 
{{out}}
 
Full disclosure: it took quite a few attempts to randomly achieve a pleasing distribution of balanced and unbalanced strings.
 
<pre> [[][]][][][[]][][] is balanced.
[][]][][[]]][][[ is unbalanced.
[]][][]][[][[] is unbalanced.
[][[[[][]]]] is balanced.
]][[][][][ is unbalanced.
]][[][][ is unbalanced.
[][][] is balanced.
[][] is balanced.
][ is unbalanced.
is balanced.</pre>
 
=={{header|R}}==
 
<langsyntaxhighlight lang="r">balanced <- function(str){
str <- strsplit(str, "")[[1]]
str <- ifelse(str=='[', 1, -1)
all(cumsum(str) >= 0) && sum(str) == 0
}</langsyntaxhighlight>
 
Alternately, using perl 5.10-compatible regexps,
 
<langsyntaxhighlight lang="r">balanced <- function(str) {
regexpr('^(\\[(?1)*\\])*$', str, perl=TRUE) > -1
}</langsyntaxhighlight>
 
To generate some some examples:
 
<langsyntaxhighlight Rlang="r">rand.parens <- function(n) paste(permutesample(c(rep('"['",n"]"),rep(']'2*n,n))replace=T),collapse="")
 
as.data.frame(within(list(), {
parens <- replicate(10, rand.parens(sample.int(10,size=1)))
balanced <- sapply(parens, balanced)
}))</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight lang="r"> balanced parens
1 FALSE ][][
2 FALSE [][[]]][[]][]]][[[
Line 3,165 ⟶ 6,896:
8 FALSE []]]][[[]][[[]
9 TRUE [[[[][[][]]]]]
10 TRUE []</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 3,187 ⟶ 6,918:
 
(for ([n 10]) (try n))
</syntaxhighlight>
</lang>
 
=={{header|REXXRaku}}==
(formerly Perl 6)
===Version 1===
There's More Than One Way To Do It.
<lang rexx>/*REXX program to check for balanced brackets [] */
===Depth counter===
@.=0
yesno.0 = left('',40) 'unbalanced'
yesno.1 = 'balanced'
 
{{works with|Rakudo|2015.12}}
q='[][][][[]]' ; call checkBal q; say yesno.result q
q='[][][][[]]][' ; call checkBal q; say yesno.result q
q='[' ; call checkBal q; say yesno.result q
q=']' ; call checkBal q; say yesno.result q
q='[]' ; call checkBal q; say yesno.result q
q='][' ; call checkBal q; say yesno.result q
q='][][' ; call checkBal q; say yesno.result q
q='[[]]' ; call checkBal q; say yesno.result q
q='[[[[[[[]]]]]]]' ; call checkBal q; say yesno.result q
q='[[[[[]]]][]' ; call checkBal q; say yesno.result q
q='[][]' ; call checkBal q; say yesno.result q
q='[]][[]' ; call checkBal q; say yesno.result q
q=']]][[[[]' ; call checkBal q; say yesno.result q
 
<syntaxhighlight lang="raku" line>sub balanced($s) {
do j=1 for 40
my $l = 0;
q=translate(rand(random(1,8)),'[]',01)
for $s.comb {
call checkBal q; if result=='-1' then iterate
say yesno.resultwhen q"]" {
end --$l;
return False if $l < 0;
exit
}
/*───────────────────────────────────PAND subroutine────────────────────*/
when "[" {
pand: p=random(0,1); return p || \p
++$l;
/*───────────────────────────────────RAND subroutine────────────────────*/
}
rand: pp=pand(); pp=pand()pp; pp=copies(pp,arg(1))
}
i=random(2,length(pp)); pp=left(pp,i-1)substr(pp,i)
return pp$l == 0;
}
/*───────────────────────────────────CHECKBAL subroutine────────────────*/
 
checkBal: procedure expose @.; arg y /*check for balanced brackets [] */
my $n = prompt "Number of brackets";
nest=0; if @.y then return '-1' /*already done this expression ? */
my $s = (<[ ]> xx $n).flat.pick(*).join;
@.y=1 /*indicate expression processed. */
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</syntaxhighlight>
do j=1 for length(y); _=substr(y,j,1) /*pick off character.*/
 
if _=='[' then nest=nest+1
===FP oriented===
else do; nest=nest-1; if nest<0 then return 0; end
Here's a more idiomatic solution using a hyperoperator to compare all the characters to a backslash (which is between the brackets in ASCII), a triangle reduction to return the running sum, a <tt>given</tt> to make that list the topic, and then a topicalized junction and a topicalized subscript to test the criteria for balance.
end /*j*/
<syntaxhighlight lang="raku" line>sub balanced($s) {
return nest==0</lang>
.none < 0 and .[*-1] == 0
'''output''' (not repeatable due to the use of RANDOM:
given ([\+] '\\' «leg« $s.comb).cache;
<pre style="height:40ex;overflow:scroll">
}
balanced [][][][[]]
 
unbalanced [][][][[]]][
my $n = prompt "Number of bracket pairs: ";
unbalanced [
my $s = <[ ]>.roll($n*2).join;
unbalanced ]
say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"</syntaxhighlight>
balanced []
 
unbalanced ][
===String munging===
unbalanced ][][
Of course, a Perl 5 programmer might just remove as many inner balanced pairs as possible and then see what's left.
balanced [[]]
{{works with|Rakudo|2015.12}}
balanced [[[[[[[]]]]]]]
<syntaxhighlight lang="raku" line>sub balanced($_ is copy) {
unbalanced [[[[[]]]][]
Nil while s:g/'[]'//;
balanced [][]
$_ eq '';
unbalanced []][[]
}
unbalanced ]]][[[[]
 
unbalanced ][[]][[]][[]][[]][[]
my $n = prompt "Number of bracket pairs: ";
unbalanced []][[]][[]][[]][[]][
my $s = <[ ]>.roll($n*2).join;
unbalanced []][
say "$s is", ' not' x not balanced($s), " well-balanced";</syntaxhighlight>
balanced [][][][][][][][][][][][][][][][]
 
unbalanced []][[]][[]][
===Parsing with a grammar===
balanced [][][][][][][][]
{{works with|Rakudo|2015.12}}
unbalanced ][][][][][][][][
<syntaxhighlight lang="raku" line>grammar BalBrack { token TOP { '[' <TOP>* ']' } }
unbalanced ][[]][[]][[]][[]][[]][[]
 
unbalanced []][[]][[]][[]][[]][[]][[]][[]][
my $n = prompt "Number of bracket pairs: ";
unbalanced ][][][][][][][][][][
my $s = ('[' xx $n, ']' xx $n).flat.pick(*).join;
unbalanced ][][][][][][][][][][][][][][
say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";</syntaxhighlight>
unbalanced ][][][][][][][][][][][][][][][][
 
unbalanced ][[]][[]][[]][[]][[]][[]][[]
=={{header|Red}}==
unbalanced []][[]][[]][[]][[]][[]][[]][
<syntaxhighlight lang="red">; Functional code
unbalanced ][][][][
balanced-brackets: [#"[" any balanced-brackets #"]"]
unbalanced []][[]][
rule: [any balanced-brackets end]
balanced [][][][][][][][][][][][]
balanced?: func [str][parse str rule]
unbalanced ][[]
 
unbalanced []][[]][[]][[]][[]][[]][
; Tests
unbalanced ][][][][][][
tests: [
balanced [][][][][][][][][][]
balanced good: []"" "[]" "[][]" "[][][]" "[][][][]" "[[[[[]]][][[]]]]"]
bad: ["[" "]" "][" "[[]" "[]]" "[]][[]" "[[[[[[]]]]]]]"]
balanced [][][][]
]
unbalanced ][[]][[]][[]][[]
 
unbalanced ][[]][[]][[]][[]][[]][[]][[]][[]
foreach str tests/good [
unbalanced ][][][][][][][][][][][][
if not balanced? str [print [mold str "failed!"]]
]
foreach str tests/bad [
if balanced? str [print [mold str "failed!"]]
]
 
repeat i 10 [
str: random copy/part "[][][][][][][][][][]" i * 2
print [mold str "is" either balanced? str ["balanced"]["unbalanced"]]
]</syntaxhighlight>
 
=={{header|REXX}}==
===with 40 examples===
<syntaxhighlight lang="rexx">/*REXX program checks for balanced brackets [ ] ─── some fixed, others random.*/
parse arg seed . /*obtain optional argument from the CL.*/
if datatype(seed,'W') then call random ,,seed /*if specified, then use as RANDOM seed*/
@.=0; yesNo.0= right('not OK', 50) /*for bad expressions, indent 50 spaces*/
yesNo.1= 'OK' /* [↓] the 14 "fixed" ][ expressions*/
q= ; call checkBal q; say yesNo.result '«null»'
q= '[][][][[]]' ; call checkBal q; say yesNo.result q
q= '[][][][[]]][' ; call checkBal q; say yesNo.result q
q= '[' ; call checkBal q; say yesNo.result q
q= ']' ; call checkBal q; say yesNo.result q
q= '[]' ; call checkBal q; say yesNo.result q
q= '][' ; call checkBal q; say yesNo.result q
q= '][][' ; call checkBal q; say yesNo.result q
q= '[[]]' ; call checkBal q; say yesNo.result q
q= '[[[[[[[]]]]]]]' ; call checkBal q; say yesNo.result q
q= '[[[[[]]]][]' ; call checkBal q; say yesNo.result q
q= '[][]' ; call checkBal q; say yesNo.result q
q= '[]][[]' ; call checkBal q; say yesNo.result q
q= ']]][[[[]' ; call checkBal q; say yesNo.result q
#=0 /*# additional random expressions*/
do j=1 until #==26 /*gen 26 unique bracket strings. */
q=translate( rand( random(1,10) ), '][', 10) /*generate random bracket string.*/
call checkBal q; if result==-1 then iterate /*skip if duplicated expression. */
say yesNo.result q /*display the result to console. */
#=#+1 /*bump the expression counter. */
end /*j*/ /* [↑] generate 26 random "Q" strings.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
?: ?=random(0,1); return ? || \? /*REXX BIF*/
rand: $=copies(?()?(),arg(1)); _=random(2,length($)); return left($,_-1)substr($,_)
/*──────────────────────────────────────────────────────────────────────────────────────*/
checkBal: procedure expose @.; parse arg y /*obtain the "bracket" expression. */
if @.y then return -1 /*Done this expression before? Skip it*/
@.y=1 /*indicate expression was processed. */
!=0; do j=1 for length(y); _=substr(y,j,1) /*get a character.*/
if _=='[' then !=!+1 /*bump the nest #.*/
else do; !=!-1; if !<0 then return 0; end
end /*j*/
return !==0 /* [↑] "!" is the nested ][ counter.*/</syntaxhighlight>
'''output''' &nbsp; using the (some internal, others random) expressions:
<pre>
OK «null»
OK [][][][[]]
not OK [][][][[]]][
not OK [
not OK ]
OK []
not OK ][
not OK ][][
OK [[]]
OK [[[[[[[]]]]]]]
not OK [[[[[]]]][]
OK [][]
not OK []][[]
not OK ]]][[[[]
not OK []][
OK [][][][][][][][][][][][]
not OK []][[]][[]][[]][[]][[]][[]][[]][
not OK []][[]][[]][[]][[]][[]][[]][[]][[]][[]][
not OK ][[]][[]
not OK ][][][][][][][][][][][][][][][][
not OK ][[]][[]][[]
not OK []][[]][
not OK ][][][][][][
OK [][][][][][]
OK [][][][][][][][][][][][][][]
OK [][][][][][][][][][][][][][][][][][][][]
not OK []][[]][[]][[]][
not OK ][[]
not OK []][[]][[]][[]][[]][[]][[]][
not OK ][[]][[]][[]][[]][[]
not OK []][[]][[]][[]][[]][[]][
OK [][][][][][][][][][][][][][][][]
not OK ][[]][[]][[]][[]][[]][[]][[]][[]
not OK ][][][][][][][][][][][][
not OK ][[]][[]][[]][[]][[]][[]][[]
not OK ][[]][[]][[]][[]][[]][[]][[]][[]][[]][[]
not OK ][][][][][][][][][][][][][][][][][][
OK [][][][][][][][]
not OK ][[]][[]][[]][[]
not OK ][][][][][][][][][][][][][][][][][][][][
</pre>
 
===Version 2===
===with examples + 30 permutations===
<lang rexx>
<syntaxhighlight lang="rexx">
/*REXX program to check for balanced brackets [] **********************
* test strings and random string generation copied from Version 1
Line 3,342 ⟶ 7,154:
end
return nest=0 /* nest=0 -> balanced */
</syntaxhighlight>
</lang>
{{out}}
===version 3===
<pre>
This REXX version (in addition to some standard examples), generates over one hundred thousand
1 10 balanced [][][][[]]
<br>unique permutations of strings that contain an equal amount of left ([) and right (]) brackets.
2 12 unbalanced [][][][[]]][
<br><br>All possible strings of twenty or less characters (brackets) are generated. This eliminates
3 1 unbalanced [
<br>the possibility of missing a particular character string permutation that may not be generated
4 1 unbalanced ]
5 2 balanced []
6 2 unbalanced ][
7 4 unbalanced ][][
8 4 balanced [[]]
9 14 balanced [[[[[[[]]]]]]]
10 11 unbalanced [[[[[]]]][]
11 4 balanced [][]
12 6 unbalanced []][[]
13 8 unbalanced ]]][[[[]
14 1 unbalanced ]
15 1 unbalanced [
16 20 unbalanced ][][][][][][][][][][
17 24 unbalanced ][][][][][][][][][][][][
18 20 unbalanced []][[]][[]][[]][[]][
19 20 balanced [][][][][][][][][][]
20 24 balanced [][][][][][][][][][][][]
21 24 unbalanced []][[]][[]][[]][[]][[]][
22 12 balanced [][][][][][]
23 32 balanced [][][][][][][][][][][][][][][][]
24 8 unbalanced []][[]][
25 32 unbalanced ][[]][[]][[]][[]][[]][[]][[]][[]
26 4 unbalanced ][[]
27 28 unbalanced ][[]][[]][[]][[]][[]][[]][[]
28 32 unbalanced ][][][][][][][][][][][][][][][][
29 28 unbalanced []][[]][[]][[]][[]][[]][[]][
30 4 unbalanced []][
</pre>
 
===with over 125,000 permutations===
This REXX version generates over one hundred thousand unique permutations of strings that contain an equal
<br>amount of left &nbsp; <big>[</big> &nbsp; and right &nbsp; <big>]</big> &nbsp; brackets.
 
All &nbsp; ''possible'' &nbsp; strings of twenty or less characters (legal bracket expressions) are generated.
 
This eliminates the possibility of missing a particular character string permutation that may not be generated
<br>via a random generator.
 
<br><br>Use is made of the '''countstr''' function (which is a BIF for newer REXX interpreters), but a
Use is made of the &nbsp; '''countstr''' &nbsp; function &nbsp; (which is a BIF for newer REXX interpreters), but a RYO version is
<br>RYO version is included here for older REXXes that don't contain that BIF.
<br>included here for older REXXes that don't contain that BIF &nbsp; ('''B'''uilt '''I'''n '''F'''unction).
<br><br>Naturally, each of the one hundred thousand character strings aren't displayed (for balanced/not-balanced),
 
Naturally, each of the one hundred thousand character strings aren't displayed (for balanced/not-balanced),
<br>but a count is displayed, as anyone can generate the same strings in other languages and compare results.
<langsyntaxhighlight lang="rexx">/*REXX program to checkchecks for around 125,000 generated balanced brackets expressions [ ] */
countbals=0
#=0; do j=1 until L>20 /*generate lots of bracket permutations*/
nested=0
q=translate( strip( x2b( d2x(j) ), 'L', 0), "][", 01) /*convert ──► ][*/
yesno.0 = left('',40) 'unbalanced'
L=length(q)
yesno.1 = 'balanced'
q='' if countStr(']', q) \== countstr('[', q) then iterate ; call checkBal q; say yesno.result q /*not compliant?*/
#=#+1 /*bump legal Q's*/
q='[][][][[]]' ; call checkBal q; say yesno.result q
!=0; do k=1 for L; parse var q ? 2 q
q='[][][][[]]][' ; call checkBal q; say yesno.result q
q='[' ; callif checkBal?=='[' q; saythen yesno.result q !=!+1
q=']' ; call checkBal q else do; say!=!-1; if !<0 then iterate j; yesno.result qend
q='[]' ; call checkBalend q; say yesno.result q/*k*/
q='][' ; call checkBal q; say yesno.result q
q='][][' ; call checkBal q; say yesno.result q
q='[[]]' ; call checkBal q; say yesno.result q
q='[[[[[[[]]]]]]]' ; call checkBal q; say yesno.result q
q='[[[[[]]]][]' ; call checkBal q; say yesno.result q
q='[][]' ; call checkBal q; say yesno.result q
q='[]][[]' ; call checkBal q; say yesno.result q
q=']]][[[[]' ; call checkBal q; say yesno.result q
call teller
count=0
nested=0
do j=1 /*generate lots of permutations. */
q=translate(strip(x2b(d2x(j)),'L',0),"][",01) /*convert──►[].*/
if countstr(']',q)\==countstr('[',q) then iterate /*compliant?*/
call checkBal q
if length(q)>20 then leave /*done all 20-char possibilities?*/
end
/*───────────────────────────────────TELLER subroutine──────────────────*/
teller: say
say count " expressions were checked, " nested ' were balanced, ',
count-nested " were unbalanced."
return
/*───────────────────────────────────CHECKBAL subroutine────────────────*/
checkBal: procedure expose nested count; parse arg y; count=count+1
nest=0
do j=1 for length(y); _=substr(y,j,1) /*pick off character.*/
select
when _=='[' then nest=nest+1 /*opening bracket ...*/
when _==']' then do; nest=nest-1; if nest<0 then leave; end
otherwise nop /*ignore any chaff. */
end /*select*/
end /*j*/
nested=nested + (nest==0)
return nest==0
/* ┌──────────────────────────────────────────────────────────────────┐
│ COUNTSTR counts the number of occurances of a string (or char)│
│ within another string (haystack) without overlap. If either arg │
│ is null, 0 (zero) is returned. To make the subroutine case │
│ insensative, change the PARSE ARG ... statement to ARG ... │
│ Example: yyy = 'The quick brown fox jumped over the lazy dog.' │
│ zz = countstr('o',yyy) /*ZZ will be set to 4 */ │
│ Note that COUNTSTR is also a built-in function of the newer │
│ REXX interpreters, and the result should be identical. Checks │
│ could be added to validate if 2 or 3 arguments are passed. │
└──────────────────────────────────────────────────────────────────┘ */
countstr: procedure; parse arg n,h,s; if s=='' then s=1; w=length(n)
do r=0 until _==0; _=pos(n,h,s); s=_+w; end; return r</lang>
'''output''' when using the default input
<pre style="overflow:scroll">
balanced
balanced [][][][[]]
unbalanced [][][][[]]][
unbalanced [
unbalanced ]
balanced []
unbalanced ][
unbalanced ][][
balanced [[]]
balanced [[[[[[[]]]]]]]
unbalanced [[[[[]]]][]
balanced [][]
unbalanced []][[]
unbalanced ]]][[[[]
 
if !==0 then bals=bals+1
14 expressions were checked, 6 were balanced, 8 were unbalanced.
end /*j*/ /*done all 20─character possibilities? */
 
125477say # " expressions were checked, " 23713 were balanced, bals 101764 ' were unbalanced.balanced, ' ,
#-bals " were unbalanced."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
countStr: procedure; parse arg n,h,s; if s=='' then s=1; w=length(n)
do r=0 until _==0; _=pos(n,h,s); s=_+w; end; return r</syntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
125476 expressions were checked, 23713 were balanced, 101763 were unbalanced.
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
nr = 0
while nr < 10
nr += 1
test=generate(random(9)+1)
see "bracket string " + test + " is " + valid(test) + nl
end
 
func generate n
l = 0 r = 0 output = ""
while l<n and r<n
switch random(2)
on 1 l+=1 output+="["
on 2 r+=1 output+="]"
off
end
if l=n output+=copy("]",n-r) else output+=copy("]",n-l) ok
return output
 
func valid q
count = 0
if len(q)=0 return "ok." ok
for x=1 to len(q)
if substr(q,x,1)="[" count+=1 else count-=1 ok
if count<0 return "not ok." ok
next
return "ok."
</syntaxhighlight>
Output:
<pre>
bracket string ]][[][[[[]]] is not ok.
bracket string [[[]]] is ok.
bracket string ]][[[[[[[]]]]] is not ok.
bracket string [][[[][[][]]][]] is ok.
bracket string [[]][][[][[][[]]]] is ok.
bracket string ]] is not ok.
bracket string [[[]]] is ok.
bracket string [][[]] is ok.
bracket string [[]] is ok.
bracket string ]]]][]]]]]]]]] is not ok.
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
1 OVER SIZE
'''WHILE''' DUP2 * 0 > '''REPEAT'''
3 PICK OVER DUP SUB
'''IF''' "[]" SWAP POS '''THEN'''
LAST 2 * 3 - ROT + SWAP '''END'''
1 -
'''END''' DROP
1 SAME "" "Not " '''IFTE''' "OK" +
≫ ''''BALBKT'''' STO
≪ { "" "[]" "[][]" "[[][]]" "][" "][][" "[]][[]" } → ts
1 ts SIZE '''FOR''' j
ts j GET '''BALBKT''' " → " SWAP + + '''NEXT'''
≫ ≫ ''''TASK'''' STO
{{out}}
<pre>
" → OK"
"[] → OK"
"[][] → OK"
"[[][]] → OK"
"][ → Not OK"
"][][ → Not OK"
"[]][[] → Not OK"
</pre>
 
Line 3,436 ⟶ 7,304:
{{trans|D}}
{{works with|Ruby|1.9}}
<langsyntaxhighlight lang="ruby">re = /\A # beginning of string
(?<bb> # begin capture group <bb>
\[ # literal [
Line 3,452 ⟶ 7,320:
t = s.gsub(/[^\[\]]/, "")
puts (t =~ re ? " OK: " : "bad: ") + s
end</langsyntaxhighlight>
 
One output: <pre>
Line 3,471 ⟶ 7,339:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">dim brk$(10)
brk$(1) = "[[[][]]]"
brk$(2) = "[[[]][[[][[][]]]]]"
Line 3,491 ⟶ 7,359:
if trim$(b$) = "" then print " OK "; else print "Not OK ";
print brk$(i)
next i</langsyntaxhighlight>
 
One output: <pre> OK
Line 3,504 ⟶ 7,372:
Not OK ][]][[
Not OK []][][][[]
</pre>
 
=={{header|Rust}}==
 
{{libheader|rand}}
<syntaxhighlight lang="rust">extern crate rand;
 
trait Balanced {
/// Returns true if the brackets are balanced
fn is_balanced(&self) -> bool;
}
 
impl<'a> Balanced for str {
fn is_balanced(&self) -> bool {
let mut count = 0;
 
for bracket in self.chars() {
let change = match bracket {
'[' => 1,
']' => -1,
_ => panic!("Strings should only contain brackets")
};
 
count += change;
if count < 0 { return false; }
}
 
count == 0
}
}
 
/// Generates random brackets
fn generate_brackets(num: usize) -> String {
use rand::random;
 
(0..num).map(|_| if random() { '[' } else { ']' }).collect()
}
 
fn main() {
for i in (0..10) {
let brackets = generate_brackets(i);
 
println!("{} {}", brackets, brackets.is_balanced())
}
}</syntaxhighlight>
Output: <pre>
true
[ false
]] false
][] false
[[[[ false
]][[[ false
[][[]] true
[]]][[] false
[[[[[[][ false
][[[[][]] false
</pre>
 
Line 3,512 ⟶ 7,436:
=== Scala Version 1 ===
{{works with|Scala|2.9.1}}
<langsyntaxhighlight lang="scala">import scala.collection.mutable.ListBuffer
import scala.util.Random
 
Line 3,547 ⟶ 7,471:
println("\n"+"check all permutations of given length:")
(1 to 5).map(generate(_)).flatten.map(s=>Pair(s,checkBalance(s))).foreach(p=>println((if(p._2) "balanced: " else "unbalanced: ")+p._1))
}</langsyntaxhighlight>
 
<pre style="height:30ex;overflow:scroll">arbitrary random order:
Line 3,923 ⟶ 7,847:
=== Scala Version 2 ===
{{works with|Scala|2.10.1}}
<langsyntaxhighlight lang="scala">import scala.util.Random.shuffle
 
object BalancedParensAppBalancedBracketsApp extends App {
 
valfor triesCount(length =<- 0 until 10) {
val str = randomBrackets(length)
 
if (is_balanced(str))
(0 until triesCount).foreach { i =>
val str = randomStringprintln(lengths"$str =- iok")
else
val msg = if (isBalanced(str)) "ok" else "NOT ok"
println(s"$str - $msgNOT ok")
}
 
def randomStringrandomBrackets(length: Int): =String {=
val parensPairs = shuffle(("[]" * length).toSeq).mkString
val parensOfGivenLength = parensPairs.take(length)
val shuffledParens = Random.shuffle(parensOfGivenLength)
shuffledParens.mkString
}
 
def isBalanced(parensStringbracketString: String): Boolean = {
var balance = 0
for (char <- bracketString) {
parensString.foreach { char =>
char match {
case '[' => balance += 1
Line 3,954 ⟶ 7,874:
}
 
}</langsyntaxhighlight>
 
Alternate implementation of "isBalanced" using tail-recursion instead of var and return:
 
<syntaxhighlight lang="scala">import scala.util.Random.shuffle
import scala.annotation.tailrec
 
// ...
 
def isBalanced(str: String): Boolean = isBalanced(str.toList, balance = 0)
 
@tailrec
def isBalanced(str: List[Char], balance: Int = 0): Boolean =
str match {
case _ if (balance < 0) => false
case Nil => balance == 0
case char :: rest =>
val newBalance = char match {
case '[' => balance + 1
case ']' => balance -1
}
isBalanced(rest, newBalance)
}
</syntaxhighlight>
 
Slightly modified implementation of "isBalanced" using tail-recursion
{{works with|Scala|2.11.7}}
<syntaxhighlight lang="scala">
@scala.annotation.tailrec
final def isBalanced(
str: List[Char],
// accumulator|indicator|flag
balance: Int = 0,
options_Map: Map[Char, Int] = Map(('[' -> 1), (']' -> -1))
): Boolean = if (balance < 0) {
// base case
false
} else {
if (str.isEmpty){
// base case
balance == 0
} else {
// recursive step
isBalanced(str.tail, balance + options_Map(str.head))
}
}
</syntaxhighlight>
 
Sample output:
Line 3,971 ⟶ 7,937:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (balanced-brackets string)
(define (b chars sum)
(cond ((and< (null? chars) (=sum 0 sum))
#f)
((and (null? chars) (= 0 sum))
#t)
((null? chars)
Line 3,979 ⟶ 7,947:
((char=? #\[ (car chars))
(b (cdr chars) (+ sum 1)))
((char=? sum#\] 0(car chars))
#f(b (cdr chars) (- sum 1)))
(else
(b (cdr chars) (- sum 1))#f)))
(b (string->list string) 0))
 
Line 3,994 ⟶ 7,962:
(balanced-brackets "][][")
(balanced-brackets "[]][[]")
</syntaxhighlight>
</lang>
 
=={{header|Scilab}}==
{{trans|MATLAB}}
<syntaxhighlight lang="text">function varargout=isbb(s)
st=strsplit(s);
t=cumsum((st=='[')-(st==']'));
balanced=and(t>=0) & t(length(t))==0;
varargout=list(balanced)
endfunction</syntaxhighlight>
{{out}}
The following code was used to generate random strings of length 5, 16, and 22 chars. It also displays the generated string, and the output (true of false) of <code>isbb()</code>.
<syntaxhighlight lang="text">for j=[5 16 22]
s=[];
for i=1:j
p=rand();
if p>0.5 then
s=s+"[";
else
s=s+"]";
end
end
disp(s);
x=isbb(s);
disp(x);
end</syntaxhighlight>
Console output:
<pre> ][]][
F
[[[[][[[][]]]]]]
T
][[][]]]][]][[]][][]]]
F</pre>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func string: generateBrackets (in integer: count) is func
Line 4,049 ⟶ 8,054:
end for;
end for;
end func;</langsyntaxhighlight>
 
Output:
Line 4,070 ⟶ 8,075:
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program balanced_brackets;
setrandom(0);
 
loop for length in [1..10] do
s := generate length;
print(balanced s, s);
end loop;
 
op generate(n);
s := "["*n + "]"*n;
loop for i in [n*2, n*2-1..2] do
j := 1 + random(i - 1);
[s(i), s(j)] := [s(j), s(i)];
end loop;
return s;
end op;
 
op balanced(s);
depth := 0;
loop for c in s do
case c of
("["):
depth +:= 1;
("]"):
depth -:= 1;
if depth<0 then return false; end if;
end case;
end loop;
return depth = 0;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>#F ][
#F ]][[
#T [[]][]
#F [[]]][][
#F ][[][][]][
#F ]][]]]][[[[[
#F ]]][[]]][[[[[]
#T [[][][[][[]][]]]
#F []][[]]]][][][[][[
#F []][]]][]][][[[][[][</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func balanced (str) {
 
var depth = 0;
str.each { |c|
if(c=='['){ depth++depth }
elsif(c==']'){ depth--depth < 0 && (return( false)) }
};
 
return (!depth);
}
 
for str [']','[','[[]','][]','[[]]','[[]]]][][]]','x[ y [ [] z ]][ 1 ][]abcd'].each {
printf("%sbalanced\t: %s\n", balanced(str) ? "" : "NOT ", str)
|str|
}</syntaxhighlight>
printf("%sbalanced\t: %s\n", balanced(str) ? "" : "NOT ", str);
};</lang>
 
{{out}}
'''Output:'''
<pre>
NOT balanced : ]
Line 4,097 ⟶ 8,144:
balanced : x[ y [ [] z ]][ 1 ][]abcd
</pre>
 
=={{header|Simula}}==
<syntaxhighlight lang="simula">BEGIN
INTEGER U;
U := ININT;
BEGIN
 
TEXT PROCEDURE GENERATE(N); INTEGER N;
BEGIN
INTEGER R;
TEXT T;
T :- NOTEXT;
WHILE N > 0 DO BEGIN
R := RANDINT(1,2,U);
T :- T & (IF R = 1 THEN "[" ELSE "]");
N := N - 1;
END;
GENERATE :- T;
END GENERATE;
 
BOOLEAN PROCEDURE BALANCED(T); TEXT T;
BEGIN
INTEGER LEVEL;
CHARACTER BRACE;
BOOLEAN DONE;
T.SETPOS(1);
WHILE T.MORE AND NOT DONE DO BEGIN
BRACE := T.GETCHAR;
IF BRACE = '[' THEN LEVEL := LEVEL + 1;
IF BRACE = ']' THEN LEVEL := LEVEL - 1;
IF LEVEL < 0 THEN DONE := TRUE;
END;
BALANCED := LEVEL = 0;
END BALANCED;
 
INTEGER I,M;
TEXT T;
FOR I := 1 STEP 1 UNTIL 40 DO BEGIN
M := RANDINT(0,10,U);
T :- GENERATE(M);
IF BALANCED(T) THEN OUTTEXT(" ") ELSE OUTTEXT(" NOT");
OUTTEXT(" BALANCED: ");
OUTTEXT(T);
OUTIMAGE;
END;
 
END;
END</syntaxhighlight>
{{in}}
<pre>710</pre>
{{out}}
<pre>
NOT BALANCED: [[[[[[][
NOT BALANCED: [[[[]
NOT BALANCED: ][
NOT BALANCED: [][[[[
BALANCED: [][[]]
NOT BALANCED: [[]
NOT BALANCED: ][
NOT BALANCED: ]][[]
NOT BALANCED: []]]
NOT BALANCED: ][]][[]
NOT BALANCED: ]]][
NOT BALANCED: ]][
NOT BALANCED: ][]]]]][]
NOT BALANCED: [[][[[]][[
NOT BALANCED: ]][]][]]
BALANCED:
NOT BALANCED: ][[
NOT BALANCED: []]][[]
NOT BALANCED: ]]]][[]]][
NOT BALANCED: ]]
BALANCED: [[][[[]]]]
NOT BALANCED: ][][]]
BALANCED:
NOT BALANCED: [[[[[]][[]
NOT BALANCED: []][[[][
NOT BALANCED: []]]][][]
NOT BALANCED: ][]][][
NOT BALANCED: []]
NOT BALANCED: ]]][[
NOT BALANCED: [
BALANCED: []
NOT BALANCED: ][]]]
NOT BALANCED: [[[[[[]][
NOT BALANCED: [][[][
NOT BALANCED: ]]
NOT BALANCED: ]][
NOT BALANCED: [[[[[[
NOT BALANCED: ]]]]]
NOT BALANCED: ]][[]
NOT BALANCED: ][][][][
</pre>
 
=={{header|Standard ML}}==
{{works with|PolyML}}
 
<syntaxhighlight lang="sml">fun isBalanced s = checkBrackets 0 (String.explode s)
and checkBrackets 0 [] = true
| checkBrackets _ [] = false
| checkBrackets ~1 _ = false
| checkBrackets counter (#"["::rest) = checkBrackets (counter + 1) rest
| checkBrackets counter (#"]"::rest) = checkBrackets (counter - 1) rest
| checkBrackets counter (_::rest) = checkBrackets counter rest</syntaxhighlight>
 
An example of usage
 
<syntaxhighlight lang="sml">val () =
List.app print
(List.map
(* Turn `true' and `false' to `OK' and `NOT OK' respectively *)
(fn s => if isBalanced s
then s ^ "\t\tOK\n"
else s ^ "\t\tNOT OK\n"
)
(* A set of strings to test *)
["", "[]", "[][]", "[[][]]", "][", "][][", "[]][[]"]
)</syntaxhighlight>
 
Output:
<pre>
OK
[] OK
[][] OK
[[][]] OK
][ NOT OK
][][ NOT OK
[]][[] NOT OK
</pre>
 
=={{header|Stata}}==
 
<syntaxhighlight lang="stata">mata
function random_brackets(n) {
return(invtokens(("[","]")[runiformint(1,2*n,1,2)],""))
}
 
function is_balanced(s) {
n = strlen(s)
if (n==0) return(1)
a = runningsum(92:-ascii(s))
return(all(a:>=0) & a[n]==0)
}
end</syntaxhighlight>
 
'''Test'''
<pre>: is_balanced("")
1
 
: is_balanced("[]")
1
 
: is_balanced("[][]")
1
 
: is_balanced("[[][]]")
1
 
: is_balanced("][")
0
 
: is_balanced("][][")
0
 
: is_balanced("[]][[]")</pre>
 
=={{header|Swift}}==
 
Checks balance function:
 
<syntaxhighlight lang="swift">import Foundation
 
func isBal(str: String) -> Bool {
var count = 0
return !str.characters.contains { ($0 == "[" ? ++count : --count) < 0 } && count == 0
}
</syntaxhighlight>output:<syntaxhighlight lang="swift">
isBal("[[[]]]") // true
 
isBal("[]][[]") // false
 
</syntaxhighlight>Random Bracket function:<syntaxhighlight lang="swift">
 
func randBrack(n: Int) -> String {
var bracks: [Character] = Array(Repeat(count: n, repeatedValue: "["))
for i in UInt32(n+1)...UInt32(n + n) {
bracks.insert("]", atIndex: Int(arc4random_uniform(i)))
}
return String(bracks)
}
 
</syntaxhighlight>output:<syntaxhighlight lang="swift">
 
randBrack(2) // "]][["
 
</syntaxhighlight>Random check balance function:<syntaxhighlight lang="swift">
 
func randIsBal(n: Int) {
let (bal, un) = ("", "un")
for str in (1...n).map(randBrack) {
print("\(str) is \(isBal(str) ? bal : un)balanced\n")
}
}
 
randIsBal(4)
 
</syntaxhighlight>output:<syntaxhighlight lang="swift">
 
// ][ is unbalanced
//
// ]][[ is unbalanced
//
// []][[] is unbalanced
//
// [][][[]] is balanced</syntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc generate {n} {
if {!$n} return
set l [lrepeat $n "\[" "\]"]
Line 4,126 ⟶ 8,401:
set s [generate $i]
puts "\"$s\"\t-> [expr {[balanced $s] ? {OK} : {NOT OK}}]"
}</langsyntaxhighlight>
Sample output:
<pre>
Line 4,147 ⟶ 8,422:
===Constructing correctly balanced strings===
It is, of course, possible to directly construct such a balanced string, this being much more useful as the length of the string to generate grows longer. This is done by conceptually building a random tree (or forest) and then walking the tree, with open brackets being appended when a node is entered from its root and close brackets being appended when a node is left for its root. This is equivalent to inserting a balanced pair of brackets at a random place in an initially-empty string <math>n</math> times, which might be done like this:
<langsyntaxhighlight lang="tcl">proc constructBalancedString {n} {
set s ""
for {set i 0} {$i < $n} {incr i} {
Line 4,154 ⟶ 8,429:
}
return $s
}</langsyntaxhighlight>
As noted, because the generated string is guaranteed to be balanced, it requires no further filtering and this results in much more efficient generation of balanced strings at longer lengths (because there's no need to backtrack).
 
=={{header|TMG}}==
Unix TMG is designed to process and generate files rather than process text in memory. Therefore generation and analysis parts can only be done in separate programs.
 
Generation program in Unix TMG:
<syntaxhighlight lang="unixtmg">program: readint(n) [n>0] readint(seed)
loop: parse(render) [--n>0?]/done loop;
render: random(i, 15) [i = (i+1)*2] loop2 = { 1 * };
loop2: random(b, 2) ( [b&1?] ={<[>} | ={<]>} ) [--i>0?]/done loop2 = { 2 1 };
done: ;
 
/* Reads decimal integer */
readint: proc(n;i) string(spaces) [n=0] inta
int1: [n = n*12+i] inta\int1;
inta: char(i) [i<72?] [(i =- 60)>=0?];
 
/* LCG params: a = 29989, c = 28411, m = 35521 */
random: proc(r,mod) [seed = (seed*72445 + 67373) % 105301]
[r = seed % mod] [r = r<0 ? -r : r];
 
spaces: <<
>>;
 
n: 0; i: 0; b: 0; seed: 0;</syntaxhighlight>
 
Sample output:
<pre>[][]
][]][]][[[]]]]][]][]
[[][]][[[]]]][[[
[]][[[[]][[]][]]][][]]
]]]][[[[</pre>
 
Analysis can be done easily using grammar specification, rather than counting brackets:
<syntaxhighlight lang="unixtmg">loop: parse(corr)\loop parse(incorr)\loop;
corr: brkts * = { < OK: > 1 * };
brkts: brkt/null brkts = { 2 1 };
brkt: <[> brkts <]> = { <[> 1 <]> };
null: = {};
 
incorr: smark ignore(<<>>) any(!<<>>) string(nonl) scopy ( * | () )
= { <NOT OK: > 1 * };
 
nonl: !<<
>>;</syntaxhighlight>
 
Sample output:
<pre>NOT OK: ][][
NOT OK: ]][][[
OK: [[]][][]
NOT OK: [[][]]][][]][]
OK: [[[]][[]][][]][]
OK: [[]][[[][]]][[[]]]
NOT OK: [[[[][[][[][[[]]][[[[[][]]
OK: [[[][[[][][[[[[]]][[][]][]][][[[]]]]]]]]</pre>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
 
Line 4,188 ⟶ 8,517:
PRINT b," ",status
ENDLOOP
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,208 ⟶ 8,537:
=={{header|TXR}}==
 
<langsyntaxhighlight lang="txr">@(define paren)@(maybe)[@(coll)@(paren)@(until)]@(end)]@(end)@(end)
@(do (defvar r (make-random-state nil))
 
(defun shuffle (list)
(for* ((vec (vector-list list))
(len (length vec))
(i 0))
((< i len) (list-vector vec))
((inc i))
(let ((j (random r len))
(temp (vecref vec i)))
(set (vecref vec i) (vecref vec j))
(set (vecref vec j) temp))))
(defun generate-1 (count)
(forlet ((i 0) chars)bkt ((<repeat i"[]" count) (cat-str (shuffle chars) "")) ((inc i))
(pushcat-str #\[(shuffle charsbkt))))
 
(push #\] chars)))
(defun generate-list (num count)
(for[[generate tf ((iop 0)generate-1 listcount)] ((< i 0..num) list) ((inc i]))
(push (generate-1 count) list))))
@(next :list @(generate-list 22 6))
@(output)
Line 4,240 ⟶ 8,559:
@{parens 15} @{matched 15} @{mismatched 15}
@ (end)
@(end)</syntaxhighlight>
</lang>
 
The recursive pattern function <code>@(paren)</code> gives rise to a grammar which matches parentheses:
Line 4,275 ⟶ 8,593:
][[[[]]]][][ ][[[[]]]][][
[[]]]]][[][[ [[]] ]]][[][[ </pre>
 
== {{header|TypeScript}} ==
{{trans|JavaScript}}
<syntaxhighlight lang="javascript">// Balanced brackets
 
function isStringBalanced(str: string): bool {
var paired = 0;
for (var i = 0; i < str.length && paired >= 0; i++) {
var c = str.charAt(i);
if (c == '[')
paired++;
else if (c == ']')
paired--;
}
return (paired == 0);
}
function generate(n: number): string {
var opensCount = 0, closesCount = 0;
// Choose at random until n of one type generated
var generated: string[] = new Array(); // Works like StringBuilder
while (opensCount < n && closesCount < n) {
if (Math.floor(Math.random() * 2) == 0) {
++opensCount;
generated.push("[");
} else {
++closesCount;
generated.push("]");
}
}
// Now pad with the remaining other brackets
generated.push(opensCount == n ?
"]".repeat(n - closesCount) :
"[".repeat(n - opensCount));
return generated.join("");
}
 
console.log("Supplied examples");
var tests: string[] = ["", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]"];
for (var test of tests)
console.log(`The string '${test}' is ${(isStringBalanced(test) ? "OK." : "not OK."));
console.log();
console.log("Random generated examples");
for (var example = 0; example < 10; example++) {
var test = generate(Math.floor(Math.random() * 10) + 1);
console.log(`The string '${test}' is ${(isStringBalanced(test) ? "OK." : "not OK.")}`);
}
</syntaxhighlight>
{{out}}
<pre>
Supplied examples
The string '' is OK.
The string '[]' is OK.
The string '][' is not OK.
The string '[][]' is OK.
The string '][][' is not OK.
The string '[[][]]' is OK.
The string '[]][[]' is not OK.
 
Random generated examples
The string ']]][[[][' is not OK.
The string '][][][[[[]]]' is not OK.
The string ']][[[[]]' is not OK.
The string '][]]]]]][][][][[[[[[' is not OK.
The string '][[[][][[[]]]]' is not OK.
The string '[[[]]]' is OK.
The string ']]]][[[[[]' is not OK.
The string '][[]]]]][[[][[]]][[[' is not OK.
The string ']]]][]][[[[[' is not OK.
The string '[[[[[[]]]]]]' is OK.
</pre>
 
=={{header|UNIX Shell}}==
{{works with|bash}}
<syntaxhighlight lang="bash">generate() {
local b=()
local i j tmp
for ((i=1; i<=$1; i++)); do
b+=( '[' ']')
done
for ((i=${#b[@]}-1; i>0; i--)); do
j=$(rand $i)
tmp=${b[j]}
b[j]=${b[i]}
b[i]=$tmp
done
local IFS=
echo "${b[*]}"
}
 
# a random number in the range [0,n)
rand() {
echo $(( $RANDOM % $1 ))
}
 
balanced() {
local -i lvl=0
local i
for ((i=0; i<${#1}; i++)); do
case ${1:i:1} in
'[') ((lvl++));;
']') (( --lvl < 0 )) && return 1;;
esac
done
(( lvl == 0 )); return $?
}
 
for ((i=0; i<=10; i++)); do
test=$(generate $i)
balanced "$test" && result=OK || result="NOT OK"
printf "%s\t%s\n" "$test" "$result"
done</syntaxhighlight>
 
{{output}}
<pre> OK
][ NOT OK
[]][ NOT OK
[[][]] OK
]][[]][[ NOT OK
[[]][[][]] OK
[]][[[[]]]][ NOT OK
[[]][]][]][[[] NOT OK
[][][[[[][][]]]] OK
[][][[[[]]][[][]]] OK
][[]][][][[[]]][[]][ NOT OK</pre>
 
=={{header|Ursala}}==
 
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 4,285 ⟶ 8,728:
#cast %bm
 
main = ^(2-$'[]'*,balanced)* eql@ZFiFX*~ iota64</langsyntaxhighlight>
output:
<pre><
Line 4,303 ⟶ 8,746:
'[[][]]': true,
'[[[]]]': true></pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Public Function checkBrackets(s As String) As Boolean
'function checks strings for balanced brackets
Line 4,371 ⟶ 8,815:
Next
End Sub
</syntaxhighlight>
</lang>
 
sample output:
Line 4,388 ⟶ 8,832:
"]][][[[][]]][][[][][": Not OK
</pre>
 
{{omit from|GUISS}}
=={{header|VBScript}}==
<syntaxhighlight lang="vb">For n = 1 To 10
sequence = Generate_Sequence(n)
WScript.Echo sequence & " is " & Check_Balance(sequence) & "."
Next
 
Function Generate_Sequence(n)
For i = 1 To n
j = Round(Rnd())
If j = 0 Then
Generate_Sequence = Generate_Sequence & "["
Else
Generate_Sequence = Generate_Sequence & "]"
End If
Next
End Function
 
Function Check_Balance(s)
Set Stack = CreateObject("System.Collections.Stack")
For i = 1 To Len(s)
char = Mid(s,i,1)
If i = 1 Or char = "[" Then
Stack.Push(char)
ElseIf Stack.Count <> 0 Then
If char = "]" And Stack.Peek = "[" Then
Stack.Pop
End If
Else
Stack.Push(char)
End If
Next
If Stack.Count > 0 Then
Check_Balance = "Not Balanced"
Else
Check_Balance = "Balanced"
End If
End Function</syntaxhighlight>
 
{{out}}
Note: For some reason, the function to generate the bracket sequence, Generate_Sequence, does not produce a balanced one. But the
function to check if it is balanced or not, Check_Balance, works if a balanced argument is passed manually.
<pre>] 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.</pre>
 
Antother version not using libraries. The generator works as intended (more difficult to do than the checking)
<syntaxhighlight lang="vb">
option explicit
 
function do_brackets(bal)
dim s,i,cnt,r
if bal then s="[":cnt=1 else s="":cnt=0
for i=1 to 20
if (rnd>0.7) then r=not r
if not r then
s=s+"[" :cnt=cnt+1
else
if not bal or (bal and cnt>0) then s=s+"]":cnt=cnt-1
end if
next
if bal and cnt<>0 then s=s&string(cnt,"]")
if not bal and cnt=0 then s=s&"]"
do_brackets=s
end function
 
function isbalanced(s)
dim s1,i,cnt: cnt=0
for i=1 to len(s)
s1=mid(s,i,1)
if s1="[" then cnt=cnt+1
if s1="]" then cnt=cnt-1
if cnt<0 then isbalanced=false:exit function
next
isbalanced=(cnt=0)
end function
 
function iif(a,b,c): if a then iif=b else iif=c end if : end function
 
randomize timer
dim i,s,bal,bb
for i=1 to 10
bal=(rnd>.5)
s=do_brackets(bal)
bb=isbalanced(s)
wscript.echo iif (bal,"","un")& "balanced string " &vbtab _
& s & vbtab & " Checks as " & iif(bb,"","un")&"balanced"
next
</syntaxhighlight>
Sample run
<pre>
unbalanced string [[[[[[[[][[][[[[[]][ Checks as unbalanced
balanced string [[[]]][[[[[[[]][]]]]]] Checks as balanced
balanced string [[[]][]][[[[[]]]]] Checks as balanced
unbalanced string ][][]][[[[]]][]]]]]] Checks as unbalanced
balanced string [[][[[[[[[[]]][[[[[[[]]]]]]]]]]]]] Checks as balanced
balanced string [[]][][[[[[]]]]] Checks as balanced
balanced string [][[]][][][[[[[[]]]]]] Checks as balanced
balanced string [[[[[[[]][[[[[[[[[][[]]]]]]]]]]]]]]] Checks as balanced
unbalanced string [[]]][[[[[[]]]]]][][] Checks as unbalanced
balanced string [[[[[[][[[[[]]]]]]]]]] Checks as balanced
</pre>
 
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang="vbnet">Module Module1
 
Private rand As New Random
 
Sub Main()
For numInputs As Integer = 1 To 10 '10 is the number of bracket sequences to test.
Dim input As String = GenerateBrackets(rand.Next(0, 5)) '5 represents the number of pairs of brackets (n)
Console.WriteLine(String.Format("{0} : {1}", input.PadLeft(10, CChar(" ")), If(IsBalanced(input) = True, "OK", "NOT OK")))
Next
Console.ReadLine()
End Sub
 
Private Function GenerateBrackets(n As Integer) As String
 
Dim randomString As String = ""
Dim numOpen, numClosed As Integer
 
Do Until numOpen = n And numClosed = n
If rand.Next(0, 501) Mod 2 = 0 AndAlso numOpen < n Then
randomString = String.Format("{0}{1}", randomString, "[")
numOpen += 1
ElseIf rand.Next(0, 501) Mod 2 <> 0 AndAlso numClosed < n Then
randomString = String.Format("{0}{1}", randomString, "]")
numClosed += 1
End If
Loop
Return randomString
End Function
 
Private Function IsBalanced(brackets As String) As Boolean
 
Dim numOpen As Integer = 0
Dim numClosed As Integer = 0
 
For Each character As Char In brackets
If character = "["c Then numOpen += 1
If character = "]"c Then
numClosed += 1
If numClosed > numOpen Then Return False
End If
Next
Return numOpen = numClosed
End Function
End Module</syntaxhighlight>
 
{{out}}
<pre>
][[][]][ : NOT OK
[]]][[[] : NOT OK
[[]][] : OK
[] : OK
[[[]]] : OK
[] : OK
[]][][ : NOT OK
]][[[] : NOT OK
: OK
[] : OK
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import datatypes as dt
 
fn is_valid(bracket string) bool {
mut s := dt.Stack<string>{}
for b in bracket.split('') {
if b == '[' {
s.push(b)
} else {
if s.peek() or {''} == '[' {
s.pop() or {panic("WON'T GET HERE EVER")}
} else {
return false
}
}
}
return true
}
 
fn main() {
brackets := ['','[]','[][]','[[][]]','][','][][','[]][[]','[][[][[]][][[][]]]']
for b in brackets {
println('$b ${is_valid(b)}')
}
}</syntaxhighlight>
 
{{out}}
<pre>
true
[] true
[][] true
[[][]] true
][ false
][][ false
[]][[] false
[][[][[]][][[][]]] true
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="wren">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")")
}</syntaxhighlight>
 
{{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>
 
=={{header|X86 Assembly}}==
<syntaxhighlight lang="x86assembly">
section .data
 
MsgBalanced: db "OK", 10
MsgBalancedLen: equ 3
 
MsgUnbalanced: db "NOT OK", 10
MsgUnbalancedLen: equ 7
 
MsgBadInput: db "BAD INPUT", 10
MsgBadInputLen: equ 10
 
Open: equ '['
Closed: equ ']'
 
section .text
 
BalancedBrackets:
 
xor rcx, rcx
mov rsi, rdi
cld
 
processBracket:
lodsb
cmp al, 0
je determineBalance
 
cmp al, Open
je processOpenBracket
 
cmp al, Closed
je processClosedBracket
 
mov rsi, MsgBadInput
mov rdx, MsgBadInputLen
jmp displayResult
 
processOpenBracket:
add rcx, 1
jmp processBracket
 
processClosedBracket:
cmp rcx, 0
je unbalanced
 
sub rcx, 1
jmp processBracket
 
 
determineBalance:
cmp rcx, 0
jne unbalanced
 
mov rsi, MsgBalanced
mov rdx, MsgBalancedLen
jmp displayResult
 
unbalanced:
mov rsi, MsgUnbalanced
mov rdx, MsgUnbalancedLen
 
displayResult:
mov rax, 1
mov rdi, 1
syscall
ret
</syntaxhighlight>
 
=={{header|XBasic}}==
{{trans|JavaScript}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">' Balanced brackets
PROGRAM "balancedbrackets"
VERSION "0.001"
 
IMPORT "xst"
 
DECLARE FUNCTION Entry()
INTERNAL FUNCTION IsStringBalanced(str$)
INTERNAL FUNCTION Generate$(n%%)
 
' Pseudo-random number generator
' Based on the rand, srand functions from Kernighan & Ritchie's book
' 'The C Programming Language'
DECLARE FUNCTION Rand()
DECLARE FUNCTION SRand(seed%%)
 
FUNCTION Entry()
PRINT "Supplied examples"
DIM tests$[6]
tests$[0] = ""
tests$[1] = "[]"
tests$[2] = "]["
tests$[3] = "[][]"
tests$[4] = "][]["
tests$[5] = "[[][]]"
tests$[6] = "[]][[]"
FOR example@@ = 0 TO UBOUND(tests$[])
test$ = tests$[example@@]
PRINT "The string '"; test$; "' is ";
IFT IsStringBalanced(test$) THEN
PRINT "OK."
ELSE
PRINT "not OK."
END IF
NEXT example@@
PRINT
PRINT "Random generated examples"
XstGetSystemTime (@msec)
SRand(INT(msec) MOD 32768)
FOR example@@ = 1 TO 10
test$ = Generate$(INT(Rand() / 32768.0 * 10.0) + 1)
PRINT "The string '"; test$; "' is ";
IFT IsStringBalanced(test$) THEN
PRINT "OK."
ELSE
PRINT "not OK."
END IF
NEXT example@@
END FUNCTION
 
FUNCTION IsStringBalanced(s$)
paired& = 0
i%% = 1
DO WHILE i%% <= LEN(s$) && paired& >= 0
c$ = MID$(s$, i%%, 1)
SELECT CASE c$
CASE "[":
INC paired&
CASE "]":
DEC paired&
END SELECT
INC i%%
LOOP
END FUNCTION (paired& = 0)
 
FUNCTION Generate$(n%%)
opensCount%% = 0
closesCount%% = 0
' Choose at random until n%% of one type generated
generated$ = ""
DO WHILE opensCount%% < n%% && closesCount%% < n%%
SELECT CASE (INT(Rand() / 32768.0 * 2.0) + 1)
CASE 1:
INC opensCount%%
generated$ = generated$ + "["
CASE 2:
INC closesCount%%
generated$ = generated$ + "]"
END SELECT
LOOP
' Now pad with the remaining other brackets
IF opensCount%% = n%% THEN
generated$ = generated$ + CHR$(']', n%% - closesCount%%)
ELSE
generated$ = generated$ + CHR$('[', n%% - opensCount%%)
END IF
END FUNCTION generated$
 
' Return pseudo-random integer on 0..32767
FUNCTION Rand()
#next&& = #next&& * 1103515245 + 12345
END FUNCTION USHORT(#next&& / 65536) MOD 32768
 
' Set seed for Rand()
FUNCTION SRand(seed%%)
#next&& = seed%%
END FUNCTION
 
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Supplied examples
The string '' is OK.
The string '[]' is OK.
The string '][' is not OK.
The string '[][]' is OK.
The string '][][' is not OK.
The string '[[][]]' is OK.
The string '[]][[]' is not OK.
 
Random generated examples
The string '[[]][][[[]]]' is OK.
The string '[]' is OK.
The string '[]]][[' is not OK.
The string ']]][][[]][][[[' is not OK.
The string ']]]]]][[[][[][[[' is not OK.
The string '[]][' is not OK.
The string ']]][[][][[' is not OK.
The string '][' is not OK.
The string '[[]][][]' is OK.
The string '[[[][[]]]]' is OK.
</pre>
 
=={{header|XBS}}==
<syntaxhighlight lang="xbs">const Chars:[string] = ["[","]"];
func GenerateString(Amount:number=4):string{
set Result:string = "";
<|(*1..Amount)=>Result+=Chars[math.random(0,?Chars-1)];
send Result;
}
 
func IsBalanced(String:string):boolean{
set Pairs:number = 0;
<|(*(0..(?String-1)))=>(String::at(_)=="[")?(Pairs<0)?=>send false,Pairs++,=>Pairs--;
send (Opens==Closes)&(Pairs==0);
}
 
repeat 10 {
set s = GenerateString(math.random(2,4)*2);
log(`{s}: {IsBalanced(s)}`);
}</syntaxhighlight>
{{out}}
<pre>
][]][]: false
][]]]]: false
][][: false
[[[[]][]: false
[[][]]: true
[[]]: true
]][][][[: false
[][]: true
][[[[]: false
[]]][]: false
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic code declarations
 
int N, I, C, Nest;
Line 4,416 ⟶ 9,349:
Text(0,"OK
");
]</langsyntaxhighlight>
 
Example output:
Line 4,426 ⟶ 9,359:
[[][]] OK
</pre>
 
=={{header|Ya}}==
<syntaxhighlight lang="ya">@Balanced[]s // each source must be started by specifying its file name; std extension .Ya could be ommitted and auto added by compiler
 
// all types are prefixed by `
// definition of anything new is prefixed by \, like \MakeNew_[]s and \len
// MakeNew_[]s is Ok ident in Ya: _ starts sign part of ident, which must be ended by _ or alphanum
`Char[^] \MakeNew_[]s(`Int+ \len) // `Char[^] and `Char[=] are arrays that owns their items, like it's usally in other langs;
// yet in assignment of `Char[^] to `Char[=] the allocated memory is moved from old to new owner, and old string becomes empty
// there are tabs at starts of many lines; these tabs specify what in C++ is {} blocks, just like in Python
len & 1 ==0 ! // it's a call to postfix function '!' which is an assert: len must be even
`Char[=] \r(len) // allocate new string of length len
// most statements are analogous to C++ but written starting by capital letter: For If Switch Ret
For `Char[] \eye = r; eye // // `Char[] is a simplest array of chars, which does not hold a memory used by array items; inc part of For loop is missed: it's Ok, and condition and init could also be missed
*eye++ = '['; *eye++ = '[' // fill r by "[][][]...". The only place with ; as statement delemiter: required because the statement is not started at new line.
// below is a shuffle of "[][][]..." array
For `Char[] \eye = r; ++eye // var eye is already defined, but being the same `Char[] it's Ok by using already exisiting var. ++eye is used: it allows use of eye[-1] inside
`Int+ \at = Random(eye/Length) // `Int+ is C++'s unsigned int. eye/Length: / is used for access to field, like in file path
eye[-1],eye[at] = eye[at],eye[-1] // swap using tuples; eye[-1] accesses char that is out of current array, yet it's allowed
Ret r // Ret is C's return
`Bool \AreBalanced(`Char[] \brackets)
`Int+ \extra = 0
For ;brackets ;++brackets
Switch *brackets
'[' // it's a C++'s 'case': both 'case' and ':' are skipped being of no value; but the code for a case should be in block, which is here specifyed by tabs at next line start
++extra
']'
If !!extra // '!!' is `Bool not, like all other `Bool ops: && || ^^
Ret No // No and False are C's false; Yes and True are C's true
--extra
// There is no default case, which is written as ':' - so if no case is Ok then it will fail just like if being written as on the next line
// : { 0! } // C's default: assert(0);
Ret extra == 0
// function ala 'main' is not used: all global code from all modules are executed; so below is what typically is in ala 'main'
For `Int \n=10; n; --n
// below note that new var 'brackets' is created inside args of func call
//@Std/StdIO/ is used here to use Print function; else it maybe changed to Use @Std/StdIO at global level before this For loop
@Std/StdIO/Print(; "%s : %s\n" ;`Char[=] \brackets = MakeNew_[]s(10) /* all bracket strings are of length 10 */; AreBalanced(brackets) ? "Ok" : "bad")
// note that starting arg of Print is missed by using ';' - default arg value is allowed to use for any arg, even if next args are written</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">sub check_brackets(s$)
local level, i
for i = 1 to len(s$)
switch mid$(s$, i, 1)
case "[": level = level + 1 : break
case "]": level = level - 1 : if level < 0 break 2
end switch
next i
return level = 0
end sub
s$ = "[[]][]"
 
print s$, " = ";
 
if not check_brackets(s$) print "not ";
print "ok"</syntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn bb(bs){ while(a:=bs.span("[","]")) {bs=bs[a[1],*]} (Void!=a) }</syntaxhighlight>
The span method finds the start and length of a balanced span. This algorithm assumes the string only contains brackets; a matched span is chopped off the front of the string and a new balanced span is searched for. Stops when the string is empty or unbalanced (span returns Void).
<pre>
zkl: bb("")
True
zkl: bb("[]")
True
zkl: bb("[][]")
True
zkl: bb("[[][]]")
True
zkl: bb("][")
False
zkl: bb("][][")
False
zkl: bb("[]][[]")
False
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<syntaxhighlight lang="zxbasic">10 FOR n=1 TO 7
20 READ s$
25 PRINT "The sequence ";s$;" is ";
30 GO SUB 1000
40 NEXT n
50 STOP
1000 LET s=0
1010 FOR k=1 TO LEN s$
1020 LET c$=s$(k)
1030 IF c$="[" THEN LET s=s+1
1040 IF c$="]" THEN LET s=s-1
1050 IF s<0 THEN PRINT "Bad!": RETURN
1060 NEXT k
1070 IF s=0 THEN PRINT "Good!": RETURN
1090 PRINT "Bad!"
1100 RETURN
2000 DATA "[]","][","][][","[][]","[][][]","[]][[]","[[[[[]]]]][][][]][]["
</syntaxhighlight>
{{omit from|GUISS}}
Anonymous user