Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
47
Task/Balanced-brackets/OoRexx/balanced-brackets.rexx
Normal file
47
Task/Balanced-brackets/OoRexx/balanced-brackets.rexx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
tests = .array~of("", "[]", "][", "[][]", "][][", "[[][]]", "[]][[]")
|
||||
|
||||
-- add some randomly generated tests
|
||||
loop i = 1 to 8
|
||||
tests~append(generateBrackets(i))
|
||||
end
|
||||
|
||||
loop test over tests
|
||||
say test":" checkbrackets(test)
|
||||
end
|
||||
|
||||
::routine checkBrackets
|
||||
use arg input
|
||||
-- counter of bracket groups. Must be 0 at end to be valid
|
||||
groups = 0
|
||||
|
||||
-- loop over all of the characters
|
||||
loop c over input~makearray("")
|
||||
if c == '[' then groups += 1
|
||||
else if c == ']' then groups -= 1
|
||||
else return .false -- non-bracket char found
|
||||
-- check for a close occurring before an open
|
||||
if groups < 0 then return .false
|
||||
end
|
||||
-- should be zero at the end
|
||||
return groups == 0
|
||||
|
||||
-- generate a string with n pairs of brackets
|
||||
::routine generateBrackets
|
||||
use arg n
|
||||
|
||||
answer = .mutablebuffer~new(,2*n)
|
||||
|
||||
openBracketsNeeded = n
|
||||
unclosedBrackets = 0
|
||||
loop while answer~length < 2 * n
|
||||
if random(0, 1) & openBracketsNeeded > 0 | unclosedBrackets == 0 then do
|
||||
answer~append('[')
|
||||
openBracketsNeeded -= 1
|
||||
unclosedBrackets += 1
|
||||
end
|
||||
else do
|
||||
answer~append(']')
|
||||
unclosedBrackets -= 1
|
||||
end
|
||||
end
|
||||
return answer~string
|
||||
Loading…
Add table
Add a link
Reference in a new issue