Balanced brackets: Difference between revisions
Add APL
Thundergnat (talk | contribs) m (→{{header|XBasic}}: Regularize non-standard header markup) |
Not a robot (talk | contribs) (Add APL) |
||
Line 598:
input is: []line 1:2 missing NEWLINE at ']'
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
These are a function to generate a random string of N brackets of each type,
and a function to check whether a given string of brackets is balanced.
<lang APL>gen ← (⊂+?+)⍨ ⌷ ('[]'/⍨⊢)
bal ← ((|≡⊢)+\ ∧ 0=+/)(+⌿1 ¯1×[1]'[]'∘.=⊢)</lang>
Sample run for 0..N..10:
<lang APL> ↑{br←gen ⍵ ⋄ br,': ',(1+bal br)⊃'Bad' 'Good'}¨0,⍳10
: Good
[]: Good
][][: Bad
[][[]]: Good
[]]][[][: Bad
][][[[[]]]: Bad
][][][][][[]: Bad
][[[][[][]][]]: Bad
[[][[]]][[[[]]]]: Good
[[[]][[[[][]][]]]]: Good
]][][][][[][][]][[[]: Bad</lang>
=={{header|AppleScript}}==
|