The replace trick of Raul is nice. After my initially failed (like Aaron's) attempt to use replace, I solved the problem in a more array oriented way:
p1 =: [: +/ ".@({.,{:)@:(#~e.&Num_j_);._2 num =: ;:'zero one two three four five six seven eight nine' fix =: [: ([: i:&1"1@|: 1,num (E.~>)~"0 1{.)}@|:,"0 1&Num_j_ p2 =: p1@fix The juicy part is "(E.~>)~"0 1" which, when passed num on the left and a string y on the right creates a boolean matrix of shape 10,#y , indicating where which number is found. The rest massages it in the form required by composite item (monadic u}) to replace the locations by digits: eighthree => 8igh3hree On Reddit, I encountered a cute alternative for my "(E.~>)~"0 1": E.S:0 . Jan-Pieter On Sat, 2 Dec 2023, 03:53 Aaron Ash, <aaron....@gmail.com> wrote: > I initially hit the same issue and then changed my approach to avoid using > stringreplace. > The logic I ended up with was: find the first and last match of any of the > digits or number words, > combine those two into a single string and now replace words with digits on > those knowing that > there's no chance of overlaps. > > > SPOILERS BELOW: > > > s =: 0 : 0 > > two1nine > > eightwothree > > abcone2threexyz > > xtwone3four > > 4nineeightseven2 > > zoneight234 > > 7pqrstsixteen > > ) > > > NB. initial "replace" verb that had the same issue > > > r=:rplc&(','cut'zero,0,one,1,two,2,three,3,four,4,five,5,six,6,seven,7,eight,8,nine,9') > > > > nums=:','cut'one,two,three,four,five,six,seven,eight,nine,1,2,3,4,5,6,7,8,9' > > NB. working "replace" verb > > r=:{{ > > f=.(nums{~]i.<./@-.&0) ,(<./@-.&0)"1 I.E.&(' ',y) &> nums > > l=.(nums{~]i.>./@-.&0) ,>./"1 I.E.&(' ',y) &> nums > > ". -.&' ' (>f,l) rplc > > (','cut'zero,0,one,1,two,2,three,3,four,4,five,5,six,6,seven,7,eight,8,nine,9') > > }} > > > +/ r &> LF cut s > > 281 > > > > > On Sat, Dec 2, 2023 at 12:29 PM David Pinchbeck <dpinc...@gmail.com> > wrote: > > > I'm trying to improve my J using Advent of Code, but am already a bit > > stuck on the second part of day 1. It seems late enough in the day now > to > > ask this question without spoiling anything. > > > > On the example code one needs to turn the string 'eightwothree' into > '8wo3' > > I tried the following, which fails because the priority of stringreplace > > comes from the left argument ordering: > > > > digs=:'0123456789' > > > > s=:'zero';'one';'two';'three';'four';'five';'six';'seven';'eight';'nine' > > > > substr=:(s,. <"0 digs) > > > > g=:{{ substr stringreplace y}} > > > > g 'eightwothree' > > > > eigh23 > > > > How can I get the earliest appearance on the right side to be replaced > > first instead? > > > > Thanks, > > David > > -- > > David Pinchbeck > > Associate Professor of Mathematics > > St. Joseph's College > > Standish, ME > > ---------------------------------------------------------------------- > > 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