Balanced brackets: Difference between revisions
Content added Content deleted
(→{{header|J}}: generate bracket pairs, rename checker) |
(→{{header|J}}: break up check for readability, note issue with uneven brackets.) |
||
Line 46: | Line 46: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''': <lang j>genBracketPairs =: ?~@+: { #&'[]' NB. bracket pairs in arbitrary order |
'''Solution''': <lang j>genBracketPairs =: ?~@+: { #&'[]' NB. bracket pairs in arbitrary order |
||
bracketDepth =: '[]' -&(+/\)/@:(=/) ] |
|||
checkBalanced =: _1 -.@e. bracketDepth</lang> |
|||
'''Examples''':<lang j> 'TESTS EXPECTED'=:( a:"_`0:`[} ,&< (<'OK') = ])/ |: cut;._2 noun define |
'''Examples''':<lang j> 'TESTS EXPECTED'=:( a:"_`0:`[} ,&< (<'OK') = ])/ |: cut;._2 noun define |
||
(empty) OK |
(empty) OK |
||
Line 72: | Line 73: | ||
EXPECTED -: checkBalanced &> TESTS |
EXPECTED -: checkBalanced &> TESTS |
||
1</lang> |
1</lang> |
||
'''Comments''': This task highlights the versatility and usefulness of J's scanning modifiers, < |
'''Comments''': This task highlights the versatility and usefulness of J's scanning modifiers, <code>/</code> and <code>\</code>. |
||
The <code>checkBalanced</code> verb would need modification if the task was extended to include uneven numbers of opening and closing brackets. |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |