I am generating possible combination of three letters like (A,B,C ) in below pattern. Fix outer letter and change inside .
For (A,B,C) = 3*3*3 possibilities .
I store all in array , now by sequence can we know in which index particular combination had occurred. Example : "ABC" at 6 the , "CCC" at 27 .
Also by index is it possible to derive in which index it appeared like 6 = "ABC" .
Is there any pattern to find out? Not expecting code only formula
A A A
A A B
A A C
A B A
A B B
A B C
A C A
A C B
A C C
B A A
B A B
B A C
B B A
B B B
B B C
B C A
B C B
B C C
C A A
C A B
C A C
C B A
C B B
C B C
C C A
C C B
C C C
Your combinations correspond to base-3 numbers, which use letters A
, B
, and C
for their digits:
A
corresponds to 0 B
corresponds to 1 C
corresponds to 2 If you convert an index to base-3, you would get the corresponding combination. You number combinations starting at 1, so you need to subtract 1 before the conversion.
For example, to get your combination for row 6 subtract 1 to get 5, then convert 5 to base-3 to get 012, then replace digits with letters to get ABC.
Similarly, you can convert a combination to base-3, eg CAB becomes 201, then convert it to decimal to get 19, add 1 to get 20 - the index of CAB in your list.
Let's look at the easy case of A and B
You'll get
AA
AB
BA
BB
But wait - that's just like binary
00
01
10
11
So how in this case - if we'll write lines we'll get:
1 = AA
2 = AB
3 = BA
4 = BB
So it's just like the binary representation of a line - 1; For example Line 2 => 2-1 = 1 => 1 in binary is 01 => AB
So in a matter of fact you should do just the same but not binary, but trinary (is that a word?) - meaning index of 3 in your example
the most right 'digit' is the alphabet size (3 in your example) in the power of 0 the next one is the alphabet size in the power of 1
So - CAC => 2*3^2 + 0*3^1 + 2*3^0 = 18 + 0 + 2 = 20 (+1 to fix that the lines start at 0) = 21 etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.