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> ' |
'''Examples''':<lang j> (,&' ' , ('bad';'OK') {::~ checkBalanced)"1 genBracketPairs i. 10 |
||
OK |
|||
[ |
][ bad |
||
][ |
][[] bad |
||
[ |
[[[]]] OK |
||
][][ |
[][[]][] OK |
||
]][ |
[][[[][]]] OK |
||
[[][]] |
[]][]][]][[[ bad |
||
[]][[] |
[[]][[][][]][] OK |
||
]]][[[ |
]]]][[][][[[[]][ bad |
||
[][][[]] |
[]]][][][[[[]][[]] 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>. |
||