new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
40
Task/Balanced-brackets/REXX/balanced-brackets-1.rexx
Normal file
40
Task/Balanced-brackets/REXX/balanced-brackets-1.rexx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*REXX program to check for balanced brackets [] */
|
||||
@.=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
|
||||
|
||||
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
|
||||
67
Task/Balanced-brackets/REXX/balanced-brackets-2.rexx
Normal file
67
Task/Balanced-brackets/REXX/balanced-brackets-2.rexx
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*REXX program to check for balanced brackets [] **********************
|
||||
* test strings and random string generation copied from Version 1
|
||||
* the rest restructured (shortened) to some extent
|
||||
* and output made reproducible (random with a seed)
|
||||
* 10.07.2012 Walter Pachl
|
||||
**********************************************************************/
|
||||
yesno.0 = 'unbalanced'
|
||||
yesno.1 = ' balanced'
|
||||
done.=0 /* memory what's been done */
|
||||
n=0 /* number of tests */
|
||||
Call testbal '[][][][[]]' /* first some user written tests */
|
||||
Call testbal '[][][][[]]]['
|
||||
Call testbal '['
|
||||
Call testbal ']'
|
||||
Call testbal '[]'
|
||||
Call testbal ']['
|
||||
Call testbal '][]['
|
||||
Call testbal '[[]]'
|
||||
Call testbal '[[[[[[[]]]]]]]'
|
||||
Call testbal '[[[[[]]]][]'
|
||||
Call testbal '[][]'
|
||||
Call testbal '[]][[]'
|
||||
Call testbal ']]][[[[]'
|
||||
Call testbal ']'
|
||||
Call testbal '['
|
||||
/* then some random generated ones */
|
||||
Call random 1,2,12345 /* call random with a seed */
|
||||
/* makes test reproducible */
|
||||
do Until n=30 /* up to 30 tests */
|
||||
s=rand(random(1,8)) /* a 01 etc. string of length 4-32 */
|
||||
q=translate(s,'[]',01) /* turn digits into brackets */
|
||||
if done.q then /* string was already here */
|
||||
iterate /* don't test again */
|
||||
call testbal q /* test balance */
|
||||
End
|
||||
exit
|
||||
|
||||
testbal: /* test the given string and show result */
|
||||
n=n+1 /* number of tests */
|
||||
Parse Arg q /* get string to be tested */
|
||||
done.q=1 /* mark as done */
|
||||
call checkBal q /* test balance */
|
||||
lq=format(length(q),2)
|
||||
say right(n,2) lq yesno.result q/* show result and string */
|
||||
Return
|
||||
|
||||
/*-----------------------------------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: procedure /*check for balanced brackets () */
|
||||
Parse arg y
|
||||
nest=0;
|
||||
do While y<>''
|
||||
Parse Var y c +1 y /*pick off one character at a time */
|
||||
if c='[' then /* opening bracket */
|
||||
nest=nest+1 /* increment nesting */
|
||||
else do /* closing bracket */
|
||||
if nest=0 then /* not allowed */
|
||||
return 0; /* no success */
|
||||
nest=nest-1 /* decrement nesting */
|
||||
end
|
||||
end
|
||||
return nest=0 /* nest=0 -> balanced */
|
||||
58
Task/Balanced-brackets/REXX/balanced-brackets-3.rexx
Normal file
58
Task/Balanced-brackets/REXX/balanced-brackets-3.rexx
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*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?*/
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue