Task:

 Generate a string with   N   opening brackets   [   and with   N   closing brackets   ],   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
   []           OK   
   [][]         OK   
   [[][]]       OK 
   ][         NOT OK
   ][][       NOT OK
   []][[]     NOT OK



*        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