|. ('()'i.n)} ')','(',: n
or tacit
([:|. ('()'&i.)`(')','(',:])}) n
On Fri Dec 18, 2020 at 10:35 PM CET, Skip Cave wrote:
> Using curlyrt: (amend in place)
>
> n=.'1+(2*3)+(4*(5+6))'
>
>
> |.(+/1 2*'()'=/n)}n,(17#')'),:(17#'(')
>
> ((6+5)*4)+(3*2)+1
>
>
>
> Skip Cave
> Cave Consulting LLC
>
>
> On Fri, Dec 18, 2020 at 12:21 PM 'Michael Day' via Programming <
> [email protected]> wrote:
>
> > Unlike in the K forum, the J-wires have been virtually silent on this
> > year's Advent of Code...
> >
> > Anyway, today's problem, number 18, might interest J & APLers as part
> > 1 is ALMOST
> > plug-it-in to J/APL; there's just one twist, so to speak, namely we
> > need to reverse an
> > arithmetic expression, eg
> > 1 + (2 * 3) + (4 * (5 + 6))
> > needs to be evaluated as
> > ((6 + 5) * 4) + (3 * 2) + 1
> >
> > But, even though all numbers are single-digit, just reversing the
> > string doesn't work, fairly obviously:
> > |.'1 + (2 * 3) + (4 * (5 + 6))'
> > ))6 + 5( * 4( + )3 * 2( + 1
> >
> > So the brackets need swapping. The data is supplied as strings, so I
> > expected to use stringreplace,
> > as found in strings.ijs
> >
> > But:
> > |. ( ')[' , '()' , '[(' ) stringreplace '1 + (2 * 3) + (4 * (5
> > + 6))'
> > [[6 + 5) * 4) + [3 * 2) + 1
> >
> > Why don't the intermediate '[' get changed to the final ')' ?
> >
> > The second action in stringreplace is on the left hand argument:
> > _2 [\ ,(')[' , '()' , '[(' )
> > )[
> > ()
> > [(
> >
> > The first column should be items of old text and the second column
> > their new replacements. But the third pair seems to be ignored.
> >
> > I did of course get round this little difficulty, but would have preferred
> > not to reinvent the wheel.
> >
> > NB. Part 2 is a little more challenging, as + is defined there as taking
> > precedence over * .
> >
> > Thanks,
> >
> > Mike
> >
> > --
> > This email has been checked for viruses by Avast antivirus software.
> > https://www.avast.com/antivirus
> >
> > ----------------------------------------------------------------------
> > 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