Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,40 +1,38 @@
/*REXX program to check for balanced brackets [] */
@.=0
yesno.0 = left('',40) 'unbalanced'
yesno.1 = 'balanced'
/*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
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
say yesno.result q
end
exit
/*───────────────────────────────────PAND subroutine────────────────────*/
pand: p=random(0,1); return p || \p
/*───────────────────────────────────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
/*───────────────────────────────────CHECKBAL subroutine────────────────*/
checkBal: procedure expose @.; arg y /*check for balanced brackets [] */
nest=0; if @.y then return '-1' /*already done this expression ? */
@.y=1 /*indicate expression processed. */
do j=1 for length(y); _=substr(y,j,1) /*pick off character.*/
if _=='[' then nest=nest+1
else do; nest=nest-1; if nest<0 then return 0; end
end /*j*/
return nest==0
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 #*/
else do; !=!-1; if !<0 then return 0; end
end /*j*/
return !==0 /* [↑] "!" is the nested counter.*/

View file

@ -1,58 +1,23 @@
/*REXX program to check for balanced brackets [ ] */
count=0
nested=0
yesno.0 = left('',40) 'unbalanced'
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
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?*/
/*REXX program checks for numerous generated balanced (square) brackets [ ] */
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
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
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
/*────────────────────────────────────────────────────────────────────────────*/
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