2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,38 +1,39 @@
|
|||
/*REXX program checks for balanced (square) brackets [ ] */
|
||||
@.=0; yesNo.0=left('',40) 'unbalanced' /*forty +1 leading blanks.*/
|
||||
yesNo.1= 'balanced'
|
||||
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
|
||||
q= ']]][[[[]' ; call checkBal q; say yesNo.result q
|
||||
|
||||
do j=1 for 40
|
||||
q=translate(rand(random(1, 8)), '[]', 01)
|
||||
call checkBal q; if result==-1 then iterate /*skip if duplicated.*/
|
||||
say yesNo.result q /*display the result.*/
|
||||
end /*j*/ /* [↑] generate 40 random "Q" strings.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
?: ?=random(0,1); return ? || \?
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
rand: ??=copies(?()?(), arg(1)); _=random(2, length(??))
|
||||
return left(??, _-1)substr(??, _)
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
checkBal: procedure expose @.; parse arg y /*get the "bracket" expression. */
|
||||
if @.y then return -1 /*already done this expression ? */
|
||||
@.y=1 /*indicate expression processed. */
|
||||
!=0; do j=1 for length(y); _=substr(y,j,1) /*get a char.*/
|
||||
if _=='[' then !=!+1 /*bump nest #*/
|
||||
/*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.*/
|
||||
return !==0 /* [↑] "!" is the nested ][ counter.*/
|
||||
|
|
|
|||
|
|
@ -1,23 +1,21 @@
|
|||
/*REXX program checks for numerous generated balanced (square) brackets [ ] */
|
||||
/*REXX program checks for around 125,000 generated balanced brackets expressions [ ] */
|
||||
bals=0
|
||||
#=0; do j=1 until length(q)>20 /*generate lots of bracket permutations*/
|
||||
q=translate(strip(x2b(d2x(j)),'L',0),"][",01) /*convert ──► []*/
|
||||
if countStr(']',q)\==countstr('[',q) then iterate /*is compliant? */
|
||||
call checkBal q
|
||||
end /*j*/ /*have all 20─character possibilities? */
|
||||
say
|
||||
say # " expressions were checked, " bals ' were balanced, ' ,
|
||||
#-bals " were unbalanced."
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
checkBal: procedure expose # bals; parse arg y; #=#+1 /*bump count.*/
|
||||
!=0
|
||||
do j=1 for length(y)
|
||||
if substr(y,j,1)=='[' then !=!+1
|
||||
else do; !=!-1; if !<0 then leave; end
|
||||
end /*j*/
|
||||
bals=bals + (!==0)
|
||||
return !==0
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
#=0; do j=1 until L>20 /*generate lots of bracket permutations*/
|
||||
q=translate( strip( x2b( d2x(j) ), 'L', 0), "][", 01) /*convert ──► ][*/
|
||||
L=length(q)
|
||||
if countStr(']', q) \== countstr('[', q) then iterate /*not compliant?*/
|
||||
#=#+1 /*bump legal Q's*/
|
||||
!=0; do k=1 for L; parse var q ? 2 q
|
||||
if ?=='[' then !=!+1
|
||||
else do; !=!-1; if !<0 then iterate j; end
|
||||
end /*k*/
|
||||
|
||||
if !==0 then bals=bals+1
|
||||
end /*j*/ /*done all 20─character possibilities? */
|
||||
|
||||
say # " expressions were checked, " bals ' were 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue