Since the result is half of the permutations, you are probably not
complaining about doing at most twice the amount of work.  Therefore I
assume you are looking for a faster way to discard the unwanted ones.  How
about:

   f=: [: {."2@~. (/:~@,: |.)"1
   g=: #~ {."1 <: {:"1

   (f -: g)@allperms"0 i. 10
1 1 1 1 1 1 1 1 1 1

i.e., the permutations you want are the ones where column 0 are less than
or equal to column n-1.





On Wed, Jun 15, 2016 at 2:42 PM, Louis de Forcrand <[email protected]> wrote:

> Hi,
>
> How would you go about generating all unique permutations of a list,
> while counting each perm and its reversal as one?
> A perhaps clearer (but less efficient) way of doing this is generating
> all permutations and then removing (reversed) duplicates:
>
>    allperms=: (A.&i.~ !) 4 NB. 4 is just an example
>    ]those_I_want=: ([: {."2@~. (/:~@,: |.)"1) allperms
> 0 1 2 3
> 0 1 3 2
> 0 2 1 3
> 0 2 3 1
> 0 3 1 2
> 0 3 2 1
> 1 0 2 3
> 1 0 3 2
> 1 2 0 3
> 1 3 0 2
> 2 0 1 3
> 2 1 0 3
>
> There is surely a better way to do this!
>
> Thanks,
> Louis
>
> ----------------------------------------------------------------------
> 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