[t=:'[]{}()' {~ 10 ?...@# 6 NB. test data
[))({}{]({
(_2[\'[]{}()') i."1 t NB. indicate different parents
0 2 2 2 2 2 2 1 2 2
2 2 2 2 0 1 0 2 2 0
2 1 1 0 2 2 2 2 0 2
[T=: * +/"1 (0 1 2,:1 _1 0) rpl (_2[\'[]{}()')i."1 t NB. indicate
situation per parent
0 1 0
(((0 < >./)*.0 <: <./)+ 2 * 0 > <./) T NB. produce required result
1
T already gives the information per parent:
0 = balanced
>0 = balanced so far
<0 = unbalanced
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [email protected] [mailto:programming-
> [email protected]] Namens Chris Burke
> Verzonden: woensdag 7 oktober 2009 7:06
> Aan: Programming forum
> Onderwerp: [Jprogramming] Balanced parens
>
> How to check if a string has balanced [] {} ()?
>
> Any pair by itself is easy enough, but I want to check all three balance
> together, e.g.
>
> ...[(])...
>
> would fail. Assume there are no character strings.
>
> Ideally, I'd like a result of
>
> 0 = balanced
> 1 = balanced so far, but some closing delimiters to come
> 2 = unbalanced (and broken)
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm