I came up with basically the same thing Henry came up with: list=: (,: <@#\) ;:0 :0 -.LF bird cow cow dog cow cow cow bird dog cat dog pig dog bird pig )
(~.@[ ,. [/.)/list ┌────┬──┬──┬──┬──┬─┐ │bird│1 │8 │14│ │ │ ├────┼──┼──┼──┼──┼─┤ │cow │2 │3 │5 │6 │7│ ├────┼──┼──┼──┼──┼─┤ │dog │4 │9 │11│13│ │ ├────┼──┼──┼──┼──┼─┤ │cat │10│ │ │ │ │ ├────┼──┼──┼──┼──┼─┤ │pig │12│15│ │ │ │ └────┴──┴──┴──┴──┴─┘ Thanks, -- Raul On Fri, Mar 4, 2016 at 5:14 AM, Henry Rich <[email protected]> wrote: > animals =: > 'bird';'cow';'cow';'dog';'cow';'cow';'cow';'bird';'dog';'cat';'dog';'pig';'dog';'bird';'pig' > > ids =: 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15 > > t =. animals,:ids > > (~.@[ ,. ]/.)/ t > > +----+--+--+--+--+-+ > > |bird|1 |8 |14| | | > > +----+--+--+--+--+-+ > > |cow |2 |3 |5 |6 |7| > > +----+--+--+--+--+-+ > > |dog |4 |9 |11|13| | > > +----+--+--+--+--+-+ > > |cat |10| | | | | > > +----+--+--+--+--+-+ > > |pig |12|15| | | | > > +----+--+--+--+--+-+ > > (~.@[ ,. <@;/.)/ t > > +----+---------+ > > |bird|1 8 14 | > > +----+---------+ > > |cow |2 3 5 6 7| > > +----+---------+ > > |dog |4 9 11 13| > > +----+---------+ > > |cat |10 | > > +----+---------+ > > |pig |12 15 | > > +----+---------+ > > > Henry Rich > > > On 3/4/2016 4:46 AM, 'Jon Hough' via Programming wrote: > >> I guess the input needs to be >> >> animals ,: ids >> >> so... >> >> ai =: animals ,: ids >> >> f2 =: (1&{) f (0&{) NB. f same as previous answer. >> >> f2 ai >> >> I guess that's not really "one expression" now, so >> >> f2 =: 1&{ (((1 ,~ #) $ ])@:~.@:] ,. ]/.~) 0&{ >> >> >> -------------------------------------------- >> On Fri, 3/4/16, 'Jon Hough' via Programming <[email protected]> >> wrote: >> >> Subject: Re: [Jprogramming] J Challenge >> To: [email protected] >> Date: Friday, March 4, 2016, 6:34 PM >> I don't think for a minute that this >> will be the most terse or most elegant but this is what I >> got: >> animals =: >> >> 'bird';'cow';'cow';'dog';'cow';'cow';'cow';'bird';'dog';'cat';'dog';'pig';'dog';'bird';'pig' >> ids =: 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15 >> f =: (((1,~ #) $])@:~.@:]) ,. (]/.)~ >> ids f animals >> -------------------------------------------- >> On Fri, 3/4/16, Skip Cave <[email protected]> >> wrote: >> Subject: [Jprogramming] J Challenge >> To: "[email protected]" >> <[email protected]> >> Date: Friday, March 4, 2016, 6:01 PM >> Well, the format of that last post >> was a bit messy. Here's a second try: >> >> The >> animal control officer wants to keep track of the >> animals >> in a city. >> The officer tags all the animals with a serial number. >> He keeps a list of all the animals he has tagged, along >> with >> each serial >> number. >> list >> ┌────┬───┬───┬───┬───┬───┬───┬────┬───┬───┬───┬───┬───┬────┬───┐ >> │bird│cow│cow│dog│cow│cow│cow│bird│dog│cat│dog│pig│dog│bird│pig│ >> ├────┼───┼───┼───┼───┼───┼───┼────┼───┼───┼───┼───┼───┼────┼───┤ >> │1 │2 │3 │4 >> │5 │6 │7 >> │8 │9 │10 │11 │12 │13 >> │14 │15 │ >> └────┴───┴───┴───┴───┴───┴───┴────┴───┴───┴───┴───┴───┴────┴───┘ >> The officer now wants to make a second list showing each >> type >> of animal along with all the serial numbers tagged for >> each >> animal. >> list2 >> ┌────┬──┬──┬──┬──┬─┐ >> │bird│1 │8 │14│ │ │ >> ├────┼──┼──┼──┼──┼─┤ >> │cow │2 │3 │5 │6 │7│ >> ├────┼──┼──┼──┼──┼─┤ >> │dog │4 │9 │11│13│ │ >> ├────┼──┼──┼──┼──┼─┤ >> │cat │10│ │ │ │ │ >> ├────┼──┼──┼──┼──┼─┤ >> │pig │12│15│ │ │ │ >> └────┴──┴──┴──┴──┴─┘ >> What is the shortest J expression to generate list2? >> (Tacit would be nice) >> >> Skip Cave >> ---------------------------------------------------------------------- >> 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 > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
