Alternative:
blnc1=: 3 : 0
z=. * +/\"1 (0 1 2,:1 _1 0) rpl (_2 [\ '[]{}()') i."1 y
(((0 ~: {:) *. 0 <: <./) + 2 * 0 > <./)"1 z
)
gives the desired output per paren.
[t=:'[{(]{]1)}([)'
[{(]{]1)}([)
blnc1 t
2 1 0
so [] is unbalanced, {} has closing delimiters to come and () is balanced.
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [email protected] [mailto:programming-
> [email protected]] Namens R.E. Boss
> Verzonden: woensdag 7 oktober 2009 12:18
> Aan: 'Programming forum'
> Onderwerp: Re: [Jprogramming] Balanced parens
>
> I gave an incomplete answer.
>
> rpl=:] - (-/ , 0:)@[ {~ {...@[ i. ] NB. from rpl2a in
> NB. http://www.jsoftware.com/pipermail/programming/2007-July/007303.html
>
> blnc0=: 3 : ' * +/"1 (0 1 2,:1 _1 0) rpl (_2[\''[]{}()'') i."1 y'
>
> blnc=: 3 : '(((0 < >./)*.0 <: <./)+ 2 * 0 > <./) blnc0 y'
>
> [t=:'[]{}()' {~ 1...@# 6
> (({(][)[)]
>
> blnc\ t
> 1 1 1 1 2 1 1 1 1 1
>
> so >./ blnc\ t is the ultimate answer.
>
> |: blnc0\ t
> 0 0 0 0 _1 0 0 1 1 0
> 0 0 1 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 1 1 1
>
> indicates where what went wrong.
>
>
> R.E. Boss
>
>
> > -----Oorspronkelijk bericht-----
> > Van: [email protected] [mailto:programming-
> > [email protected]] Namens R.E. Boss
> > Verzonden: woensdag 7 oktober 2009 11:59
> > Aan: 'Programming forum'
> > Onderwerp: Re: [Jprogramming] Balanced parens
> >
> >
> > [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
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm