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
