Balanced brackets: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: genBracketPairs handles array argument)
(→‎{{header|J}}: Generate & test pairs)
Line 41: Line 41:
bracketDepth =: '[]' -&(+/\)/@:(=/) ]
bracketDepth =: '[]' -&(+/\)/@:(=/) ]
checkBalanced =: _1 -.@e. bracketDepth</lang>
checkBalanced =: _1 -.@e. bracketDepth</lang>
'''Examples''':<lang j> 'TESTS EXPECTED'=:( a:"_`0:`[} ,&< (<'OK') = ])/ |: cut;._2 noun define
'''Examples''':<lang j> (,&' ' , ('bad';'OK') {::~ checkBalanced)"1 genBracketPairs i. 10
(empty) OK
OK
[] OK
][ bad
][ BAD
][[] bad
[][] OK
[[[]]] OK
][][ BAD
[][[]][] OK
]][[ BAD
[][[[][]]] OK
[[][]] OK
[]][]][]][[[ bad
[]][[] BAD
[[]][[][][]][] OK
]]][[[ BAD
]]]][[][][[[[]][ bad
[][][[]] OK
[]]][][][[[[]][[]] bad</lang>
[[[][[]]]] OK
[][][[][]][] OK
[[]][]]]][[[][ BAD
][][[][][][[]]][ BAD
][[[][]][]]][][[][ BAD
]][][]][[][[][[]][][ BAD
[[[][[][]]][]]][[]]][[ BAD
[[]]][]][[[[]]][[][][[]] BAD
][[][][]][[[]][[[[][]]]][] BAD
]][[][[][[[[]][[][]][[]]]]][ BAD
)

EXPECTED -: checkBalanced &> TESTS
1</lang>
'''Comments''': This task highlights the versatility and usefulness of J's scanning modifiers, <code>/</code> and <code>\</code>.
'''Comments''': This task highlights the versatility and usefulness of J's scanning modifiers, <code>/</code> and <code>\</code>.