R
R
Rrooom2014-08-21 21:54:54
Programming
Rrooom, 2014-08-21 21:54:54

How to find all combinations of characters?

There is an array of characters - ['a', 'b', 'c'].
You need to make permutations (?) of them in order to get all variants of 3x3 matrices from these symbols:

[ ['a', 'a', 'a'], ['a', 'a', 'a'], ['a', 'a', 'a']], 
,
, 
...,
 [ ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']], 
...,
 [ ['c', 'c', 'c'], ['c', 'c', 'c'], ['c', 'c', 'c']]

Can you tell the algorithm how to do it? And also poke your nose into the theory?
UPD.
I found what I needed in the python standard library:
print list(itertools.product(['a', 'b', 'c'], repeat=9))

But the sadness of the situation is that I do not understand how this can be done manually. Lack of knowledge..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mrrl, 2014-08-22
@Rrooom

Such matrices seem to describe possible binary operations on the set {'a','b','c'}, i.e. functions of two variables from this set, taking values ​​in the same set.
There will be 3^9=19683 such functions in total. Therefore, it is enough to sort through the numbers from 0 to 3 ^ 9-1, and turn the ternary record of each of them into a matrix. In C# it would look like this:

char[,] matr=new char[3,3];
    for(int i=0;i<19683;i++){
        int a=i;
        for(int j=9;--j>=0;){
            matr[j/3,j%3]=(char)('a'+a%3);
            a/=3;
        }
        Process(matr);
    }

A
Andrew, 2014-08-21
@OLS

From such a task, I would decide that at first they are interested in all permutations inside this row (selection without return) - there will be 6 of them.
And then from the resulting 6 rows, make up all possible variants of square matrices (selection with return) - respectively, there will be 6 ^ 3 = 216.
The lexicographical first square matrix will be [abc, abc, abc], then [abc, abc, acb], [abc, abc, bac], ..., and it will end with [cba, cba, cab], [cba, cba , cba].

I
Ilya Lesnykh, 2014-08-22
@Aliance

Implementation of a roughly similar algorithm in PHP: What is the best way to make a code for translation?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question