I
I
Ivan Zhukov2014-12-16 11:04:32
Arrays
Ivan Zhukov, 2014-12-16 11:04:32

How to group elements of two arrays using multiple rules (complex conditions)?

Good afternoon.
Faced with the compilation of difficult conditions and finally got confused and broke the "skis" (they do not go). There are two arrays:

string[] template = { "C", "F", "D", "J", "K", "A", "R", "W" };//шаблон для сравнения и группировки 
string[] array =    { "C", "D", "F", "J", "K", "A", "W", "R" }; //тот из которого надо составить группы

I'm trying to bring a group from array[], to the following form:
  1. "C" is the first group
  2. "D", "F" - the second group, since in the template, these elements are swapped
  3. "J", "K" - the third group, because in the template, these elements are not swapped
  4. "K", "A" - the fourth group, in the template, these elements are not swapped
  5. "W", "R" - the fifth group, in the template, these elements are swapped, but stand side by side

Here is the actual code in which I got confused by the ball (please do not kick the ball, I'm learning):
List<string> list = new List<string>();
string[] template = { "C", "F", "D", "J", "K", "A", "R", "W" };//шаблон для сравнения и группировки 
string[] array =    { "C", "D", "F", "J", "K", "A", "W", "R" }; //тот из которого надо составить группы

            bool g1 = (array[1] == template[0] || array[1] == template[1]);
            bool g1_1 = (array[1] == template[2]);
/*-------------------------------------------------------------------------------------------------*/
            bool g2 = (array[2] == template[1] || array[2] == template[2]);
            bool g2_1 = (array[2] == template[3]);
/*-------------------------------------------------------------------------------------------------*/
            bool g3 = (array[3]==template[2]||array[3]==template[3]);
            bool g3_1 = (array[3] == template[4]);
/*-----------------------------------------------------------------------------------------------------*/
            bool g4 = (array[4] == template[3] || array[4] == template[4]);
            bool g4_1 = (array[4] == template[5]);
/*----------------------------------------------------------------------------------------------------*/
            bool g5 = (array[5] == template[4] || array[5] == template[5]);
            bool g5_1 = (array[5] == template[6]);
/*--------------------------------------------------------------------------------------------------*/
            bool g6 = (array[6] == template[5] || array[6] == template[6]);
            bool g6_1 = (array[6] == template[7]);
/*--------------------------------------------------------------------------------------------------*/
            bool g7 = (array[7] == template[6] || array[7] == template[7]);

            list.Add(array[0]);

            if (g1) { list.Add(array[1]);} 
            if (g1_1 & g2) { list.Add(array[1] + "-" + array[2]); }
            if (g2_1 & g3) { list.Add(array[2] + "-" + array[3]); }
            if (g3_1 & g4) { list.Add(array[3] + "-" + array[4]); }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AigizK, 2014-12-16
@AigizK

Well, first you need to write an algorithm. If I'm not mistaken, you have it like this:
1. In the cycle, we go over the elements.
2. We take the current element and the next one from template and the current element and the next one from array
3. If they contain the same letters, then we create groups, the order is not important
4. If they are different, then we compare the current letter from template and array. If they are equal, then create a group.
But then one moment is not clear, why after the 4th step of yours a group is not created only from the letter "A"?
It is quite possible that one of the restrictions applies:
1. if the letter was used in another group, then a separate group cannot be created based on it
2. if the letter is not at the beginning of the word, then it is impossible to create a group of only 1 element

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question