Message Boards Message Boards

0
|
5450 Views
|
10 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to find list that contains different digits.

Posted 9 years ago

Hi All,

I have question. Assume I have 9 digits, i,e, 1,2,...,9. I want 3 subsets such that each 3 digit number contains digits only ones. Ex: {123,456,789},{123,476,589}, {132,976,845},{321,786,594} so on. My attempt was.

n = FromDigits /@Permutations[Range@9, {3}]
Flatten[Table[{n[[i]], n[[j]], n[[k]]}, {i, Length@n}, {j, 
   Length@n}, {k, Length@n}], 2]

But this does not give me what I want.. My another try was:

In[64]:= n1 = FromDigits /@ Permutations[Range[1, 3], {3}]

Out[64]= {123, 132, 213, 231, 312, 321}

In[65]:= n2 = FromDigits /@ Permutations[Range[4, 6], {3}]

Out[65]= {456, 465, 546, 564, 645, 654}

In[66]:= n3 = FromDigits /@ Permutations[Range[7, 9], {3}]

Out[66]= {789, 798, 879, 897, 978, 987}


 Flatten[Table[{n1[[i]], n2[[j]], n3[[k]]}, {i, Length@n1}, {j, 
       Length@n2}, {k, Length@n3}], 2]

This works but does not cover all permutations.. And I tried this one. But still no luck.

In[1]:= n1 = FromDigits /@ Permutations[{6, 2, 4}, {3}]

Out[1]={624, 642, 264, 246, 462, 426}

In[2]:= n2 = FromDigits /@ Permutations[{5, 7, 8}, {3}]

Out[2]= {578, 587, 758, 785, 857, 875}

In[3]:= n3 = FromDigits /@ Permutations[{3, 1, 9}, {3}]

Out[3]= {319, 391, 139, 193, 931, 913}

Flatten[Table[{n1[[i]], n2[[j]], n3[[k]]}, {i, Length@n1}, {j, 
   Length@n2}, {k, Length@n3}], 2]

Any suggestion? Thanks in advance..

POSTED BY: Okkes Dulgerci
10 Replies
Posted 9 years ago

I figured it out in this way..

a = Permutations[Range@9, {9}];

b = Table[Partition[a[[i]], {3}], {i, Length@a}];

n = Table[FromDigits /@ b[[i]], {i, Length@b}]

Thanks for your helps..

POSTED BY: Okkes Dulgerci
Posted 9 years ago
n = Partition[ FromDigits /@ Partition[Flatten@Permutations@Range@9, 3], 3];

 DeleteDuplicates[Sort /@ n]
POSTED BY: Okkes Dulgerci
Posted 9 years ago

Maybe this is what you are looking for

Partition[FromDigits /@ Partition[Flatten[Permutations[Range[9]]], 3],
  3]
POSTED BY: Paul Cleary
Posted 9 years ago

Yes, I did that way. After I posted my solution I saw your solution, page was open :)

POSTED BY: Okkes Dulgerci
Posted 9 years ago

Still there are duplicated sets. Note that {123,456,789}={789,456,123}

POSTED BY: Okkes Dulgerci
Posted 9 years ago

Hi again, I guess there is a minor issue here. When I convert to digit, I cannot see, for example, this triple {132, 456, 879}

FromDigits/@Append[#,Complement[Range[9],Flatten[#]]]&/@all

First and Last set identically the same, there are more repetitions. Any suggestions. Thanks.

POSTED BY: Okkes Dulgerci
Posted 9 years ago
a = Subsets[Range[9], {3}];

b = Subsets[Complement[Range[9], #], {3}] & /@ a;

c = Flatten[MapThread[Tuples[{{#1}, #2}] &, {a, b}], 1];

n = DeleteDuplicates[
   Sort@Append[#, Complement[Range[9], Flatten[#]]] & /@ c];

d = Table[Permutations /@ n[[i]], {i, Length@n}];

m = Flatten[
   Table[Table[{d[[s, 1, i]], d[[s, 2, j]], d[[s, 3, k]]}, {i, 6}, {j,
       6}, {k, 6}], {s, Length@d}], 3];

Length@m

60480

p = Table[FromDigits /@ m[[i]], {i, Length@m}]

This is what I got after I modified your code but still I am not sure this cover all possibilities..

POSTED BY: Okkes Dulgerci
Posted 9 years ago

Thank you both for quick reply! This is awesome..

POSTED BY: Okkes Dulgerci

First of all, I calculated the number of groups we want to find:

enter image description here

This will be a check for later... So here is the code to generate all of them. Basically I take a 3 numbers for the first group. then for each possible group I check the remaining digits and take 3 of those for each possible group. After that we have 3 remaining for the third group.

a=Subsets[Range[9],{3}];
b=Subsets[Complement[Range[9],#],{3}]&/@a;
all=Flatten[MapThread[Tuples[{{#1},#2}]&,{a,b}],1];
all=Append[#,Complement[Range[9],Flatten[#]]]&/@all;

And@@EqualTo[Range[9]]/@Sort/@Flatten/@all  (* all have digits 1-9 *)
1680==Length[all]==Length[DeleteDuplicates[Map[Sort,all,{2}]]]  (* all distinct and 1680 *)

Last two lines are for verification and should return True and True. And it works ;-)

POSTED BY: Sander Huisman
FromDigits /@ Partition[RandomSample[Range[9]], 3]

yielding

{319, 584, 276}

The power here is doing your randomisation before you cut the data up into substructures.

POSTED BY: David Gathercole
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract