With that description, I think I might do something like this:

   nparts=: ~.@([: /:~"1 i.@] </."1~ [ #.^:_1 [ i.@^ ])
   2 nparts 3
┌───┬─────┐
│   │0 1 2│
├───┼─────┤
│0 1│2    │
├───┼─────┤
│0 2│1    │
├───┼─────┤
│0  │1 2  │
└───┴─────┘

I should be able to do something more efficient, but before I attempt
that, I would like to clarify something:

In your examples, you do not have any empty partitions, so is that a
part of the specification also?

I am unsure if I should be paying close attention to your examples
because you said "The order of the objects in each partition is not
important" but your examples also omit partitions which contain the
objects out of order.

Actually... there's several kinds of order here which we could be discussing:

(1) the order of objects within a partition.
(2) the order of objects across partitions.
(3) the order of partitions.

In other words:

NB. (1) the order of objects within a partition
   \:~each 2 nparts 3
┌───┬─────┐
│   │2 1 0│
├───┼─────┤
│1 0│2    │
├───┼─────┤
│2 0│1    │
├───┼─────┤
│0  │2 1  │
└───┴─────┘

NB. (2) the order of objects across partitions
   2 ~.@([: \:~"1 i.@] </."1~ [ #.^:_1 [ i.@^ ]) 3
┌─────┬───┐
│0 1 2│   │
├─────┼───┤
│2    │0 1│
├─────┼───┤
│1    │0 2│
├─────┼───┤
│1 2  │0  │
└─────┴───┘

NB. (3) the order of partitions
   2 ((i.@[ ,"1 [ #.^:_1 i.@^) <@}./."1 {. , i.@]) 3
┌─────┬─────┐
│0 1 2│     │
├─────┼─────┤
│0 1  │2    │
├─────┼─────┤
│0 2  │1    │
├─────┼─────┤
│0    │1 2  │
├─────┼─────┤
│1 2  │0    │
├─────┼─────┤
│1    │0 2  │
├─────┼─────┤
│2    │0 1  │
├─────┼─────┤
│     │0 1 2│
└─────┴─────┘

I have presumed that you are thinking of both the partition contents
and the partitions themselves as sets. In other words, I think that
none of these orders matter. But... this kind of thing is worth
verifying?

Thanks,

-- 
Raul

On Fri, Oct 20, 2017 at 12:19 AM, 'Skip Cave' via Programming
<[email protected]> wrote:
> Mike,
>
> I wasn't very thorough in my definition of the original problem. I thought
> that the example I gave was enough to clarify the requirements, but looking
> back, more definition would have been good.
>
> The original problem I posted was to develop a dyadic verb that would take
> y objects and show all the ways that those y objects could be partitioned
> into x groups. Each partition set must include all objects exactly once.
> Duplication of objects is not allowed. The order of the objects in each
> partition is not important.
>
> Erling got the right idea in his previous post:
>
>   par=: 4 : '(1,.2</\"1(i.x)#/~(y=+/"1 o)#o=.((x$v)#:i.v^x){1+i.v=.1+
> y-x)<;.1[1+i.y'
>
>    2 par 3
> ┌───┬───┐
> │1  │2 3│
> ├───┼───┤
> │1 2│3  │
> └───┴───┘
>    2 par 4
> ┌─────┬─────┐
> │1    │2 3 4│
> ├─────┼─────┤
> │1 2  │3 4  │
> ├─────┼─────┤
> │1 2 3│4    │
> └─────┴─────┘
>    2 par 5
> ┌───────┬───────┐
> │1      │2 3 4 5│
> ├───────┼───────┤
> │1 2    │3 4 5  │
> ├───────┼───────┤
> │1 2 3  │4 5    │
> ├───────┼───────┤
> │1 2 3 4│5      │
> └───────┴───────┘
>    3 par 4
> ┌───┬───┬───┐
> │1  │2  │3 4│
> ├───┼───┼───┤
> │1  │2 3│4  │
> ├───┼───┼───┤
> │1 2│3  │4  │
> └───┴───┴───┘
>    3 par 5
> ┌─────┬─────┬─────┐
> │1    │2    │3 4 5│
> ├─────┼─────┼─────┤
> │1    │2 3  │4 5  │
> ├─────┼─────┼─────┤
> │1    │2 3 4│5    │
> ├─────┼─────┼─────┤
> │1 2  │3    │4 5  │
> ├─────┼─────┼─────┤
> │1 2  │3 4  │5    │
> ├─────┼─────┼─────┤
> │1 2 3│4    │5    │
> └─────┴─────┴─────┘
>    3 par 6
> ┌───────┬───────┬───────┐
> │1      │2      │3 4 5 6│
> ├───────┼───────┼───────┤
> │1      │2 3    │4 5 6  │
> ├───────┼───────┼───────┤
> │1      │2 3 4  │5 6    │
> ├───────┼───────┼───────┤
> │1      │2 3 4 5│6      │
> ├───────┼───────┼───────┤
> │1 2    │3      │4 5 6  │
> ├───────┼───────┼───────┤
> │1 2    │3 4    │5 6    │
> ├───────┼───────┼───────┤
> │1 2    │3 4 5  │6      │
> ├───────┼───────┼───────┤
> │1 2 3  │4      │5 6    │
> ├───────┼───────┼───────┤
> │1 2 3  │4 5    │6      │
> ├───────┼───────┼───────┤
> │1 2 3 4│5      │6      │
> └───────┴───────┴───────┘
>
> Skip Cave
> Cave Consulting LLC
>
> On Thu, Oct 19, 2017 at 5:47 PM, 'Mike Day' via Programming <
> [email protected]> wrote:
>
>> Skip,  in your actual Quora Problem,  why not include other triads,
>> such as 1 1 24,  2 3 4 etc;   or, otherwise,  why include both 2 4 3 and 4
>> 2 3 ?
>>
>> Anyway,  this is (quite) short and brutish but not too nasty to solve your
>> Quora problem for quite small numbers and numbers of factors:
>>
>>    |: 24 ([ ( (= */"1)#])  [:>:[#.inv i.@^ ) 3  NB. transpose gratuitous!
>>  1  1 1 1 1 1  1  1  2 2 2 2 2  2 3 3 3 3 4 4 4 4 6 6 6 8 8 12 12 24
>>  1  2 3 4 6 8 12 24  1 2 3 4 6 12 1 2 4 8 1 2 3 6 1 2 4 1 3  1  2 1
>> 24 12 8 6 4 3  2  1 12 6 4 3 2  1 8 4 2 1 6 3 2 1 4 2 1 3 1  2  1 1
>>
>> It builds triads 1 1 1 , 1 1 2 ,...1 1 24, ... up to 24 24 24, and keeps
>>
>> just those whose product is 24.
>>
>>
>> No points for space or time, filtering 30 out of 13824 candidates, but it's
>>
>> quite straightforward,  and it does yield all 30 permutations,  which some
>>
>> of the Quora corresondents appear to consider the requirement.
>>
>>
>> NB - it's not clear to me what the problem actually is - is 30 the required
>>
>> answer (number of permutations of 3 suitable factors),  or 6 (number of
>>
>> combinations of same)?
>>
>>
>> Mike
>>
>>
>>
>>
>>>
>>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to