Group Abstract Group Abstract

Message Boards Message Boards

0
|
74 Views
|
3 Replies
|
1 Total Like
View groups...
Share
Share this post:

Eliminating duplicate pairings in permutation-based partitions

Posted 2 days ago
Union[Map[Sort, TakeList[#, {2, 2}] & /@ Permutations@Range@4, {2}]]

Running the above code yields the following result:

{{{1, 2}, {3, 4}}, {{1, 3}, {2, 4}}, {{1, 4}, {2, 3}}, {{2, 3}, {1, 
   4}}, {{2, 4}, {1, 3}}, {{3, 4}, {1, 2}}}

How can I add/modify the code to further obtain the desired result as follows?

{{{1, 2}, {3, 4}}, {{1, 3}, {2, 4}}, {{1, 4}, {2, 3}}}
POSTED BY: Jim Clinton
3 Replies
Posted 1 day ago

You could use DeleteDuplicatesBy

pairs=Union[Map[Sort,TakeList[#,{2,2}]&/@Permutations@Range@4,{2}]];
DeleteDuplicatesBy[pairs,Sort]
(* {{{1,2},{3,4}},{{1,3},{2,4}},{{1,4},{2,3}}} *)

With the operator form of DeleteDuplicatesBy

Union[Map[Sort,TakeList[#,{2,2}]&/@Permutations@Range@4,{2}]]//DeleteDuplicatesBy[Sort]
POSTED BY: Hans Milton
Posted 2 days ago

You could do something like this:

DeleteDuplicates[
  Permutations@Range@4, 
  ContainsAll[#1[[1 ;; 2]], #2[[1 ;; 2]]] || ContainsNone[#1[[1 ;; 2]], #2[[1 ;; 2]]] &]
(* {{1, 2, 3, 4}, {1, 3, 2, 4}, {1, 4, 2, 3}} *)

Then you can use TakeList or whatever to reshape into pairs of pairs.

POSTED BY: Eric Rimbey
Posted 2 days ago

Thanks for your help!

DeleteDuplicates[Permutations@Range@4, 
 ContainsAll[#1[[1 ;; 2]], #2[[1 ;; 2]]] || 
   ContainsNone[#1[[1 ;; 2]], #2[[1 ;; 2]]] &]
TakeList[#, {2, 2}] & /@ %

Or

DeleteDuplicates[Permutations@Range@4, 
 ContainsAll[#1[[1 ;; 2]], #2[[1 ;; 2]]] || 
   ContainsNone[#1[[1 ;; 2]], #2[[1 ;; 2]]] &]
Partition[#, 2] & /@ %
POSTED BY: Jim Clinton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard