Answer the question
In order to leave comments, you need to log in
How to mathematically describe the enumeration of a code of 7 digits?
Greetings!
There is a code of 7 digits from 1 to 4 (1, 2, 3, 4)
It is necessary to create an array with possible combinations.
At the same time, the combination must contain at least 2 digits and they must be repeated at least twice.
The beginning of the array for clarity:
$array = ;
Answer the question
In order to leave comments, you need to log in
Download File Gen
Approximately the same as I understood, looking at night, of course, I do not generate a method, and its definition. But if no one suggests how to warm up, I'll shake the old days .. with a matan)
--------
UPD: I found an example in php. I remade it a little for the task, maybe I made a mistake where, I think PHP people will see right away if something is wrong.
char abc[] = new char[]{'1','2','3','4'};//множество допустимых символов
int size = 7;//кол-во элементов
int arr[] = new int[size];//массив для хранения текущего варианта множества
outer: while(true){//вечный цикл
//вывод варианта множества на экран
for(int ndx : arr){
System.out.print(abc[ndx]);
}
System.out.println();
int i = size - 1;//ставим курсов в самую правую ячейку
while(arr[i] == abc.length - 1){//движемся влево, если ячейка переполнена
arr[i] = 0;//записываем в ячейку 0, т.к. идет перенос разряда
i--;//сдвиг влево
//если перенос влево невозможен, значит перебор закончен
if(i < 0)break outer;
}
arr[i]++;//увеличиваем значение ячейки на единицу
}
I will answer in Haskell
codes = [c |
c <- replicateM 7 [1..4],
length (nub $ sort c) >= 2, -- как минимум 2 цифры используется
all ((>= 2) . length) $ group $ sort c] -- каждой цифры минимум 2 штуки
"at least 2 digits and they must be repeated at least twice"
If there are exactly two different digits, then it is clear what is meant. And if more? For example, is the code 1122234, in which two digits are repeated at least two times, but the other two are not, good?
I'm afraid that only direct enumeration is suitable. In C it would look like this:
char s[8],stat[4];
int i,j,k;
s[7]='\0';
for(i=0;i<16384;i++){
k=i;
for(j=0;j<4;j++) stat[j]=0;
for(j=0;j<7;j++){
stat[k%4]++;
s[j]=(char)((k%4)+'1');
k/=4;
}
if((stat[0]>=2)+(stat[1]>=2)+(stat[2]>=2)+(stat[3]>=2)>=2) printf("%s\n",s);
}
If you answer from a theoretical point of view, then the following approach may be suboptimal:
1) generate all possible combinations of numbers (1,2,3,4) from 7 elements with repetitions (to be honest, I did not fully understand if there is a limitation in your task for the number of repetitions).
2) For each of the combinations, apply the Narayana Algorithm ( https://ru.wikipedia.org/wiki/Narayana_Algorithm) to generate permutations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question