Building a matrix of combinations

I am sure that this has been asked millions of times, but when I looked for all the examples, it didn’t seem like me at all, so I thought I should ask about it anyway.

I have two arrays that will always contain 6 elements each. For instance:

string[] Colors=
    new string[] { "red", "orange", "yellow", "green", "blue", "purple" };

string[] Foods=
    new string[] { "fruit", "grain", "dairy", "meat", "sweet", "vegetable" };

Between these two arrays there are 36 possible combinations (for example, “red fruit”, “red grain”).

Now I need to further group them into a set of six unique values.

For instance:

meal[0]=
    new Pair[] { 
        new Pair { One="red", Two="fruit" }, 
        new Pair { One="orange", Two="grain" }, 
        new Pair { One="yellow", Two="dairy" }, 
        new Pair { One="green", Two="meat" }, 
        new Pair { One="blue", Two="sweet" }, 
        new Pair { One="purple", Two="vegetable" } 
    };

where is the food

Pair[][] meal;

No item can be repeated in my list of "dishes". Thus, there is only one “red” element and one “meat” item, etc.

, , .

+5
3

, , 720 . , .

, . :

  • zipped

, , .

:

http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

, :

Permutations<string> permutations = new Permutations<string>(foods);

? . , , . List<Pair>, , , .

IEnumerable<List<Pair>> query = 
    from permutation in permutations
    select colors.Zip(permutation, (color, food)=>new Pair(color, food)).ToList();

;

List<List<Pair>> results = query.ToList();

. 720 . 6 .

, ; , , .

(' LINQ, !)

+5

720 , . , 720 . .

. , . .


. Shuffle Fischer-Yates-Knuth; , StackOverflow. , LINQ, .

, , . , , .

, , - . , .

, :

Random random = new Random();
IEnumerable<string> shuffled = from food in foods 
                               orderby random.NextDouble() 
                               select food;

:

  • , - , . , .
  • Random , .
  • , - .
  • .

zip-join :

IEnumerable<Pair> results = colors.Zip(shuffled, (color, food)=>new Pair(color, food));

, , . , .

, . .

Pair[] finalResults = results.ToArray();

peasy.

+4

, . , , # , , . , .

, . . , , , , .

Fisher-Yates , . . , ? ...

    static Random rdm = new Random();
    public string[] Shuffle(string[] c)
    {
        var random = rdm;
        for (int i = c.Length; i > 1; i--)
        {
            int iRdm = rdm.Next(i);
            string cTemp = c[iRdm];
            c[iRdm] = c[i - 1];
            c[i - 1] = cTemp;
        }
        return c;
    }

: Fisher-Yates Shuffle

. , , .

, 0,1,2 .. . . Pair, , . ..... [3] [3]

 public class Pair
{
     public string One;
     public string Two;
     public Pair(string m1, string m2)
     {
         One = m1;
         Two = m2;
     }
}

, , .

, ...

Pair temp = new Pair(Colors[0],Foods[0]);

, , .

    Pair[] meal = new Pair[Colors.Length - 1];
    for (int i = 0; i < Colors.Length - 1; i++)
    {
        meal[i] = new Pair(Colors[i],Foods[i]);
    } 

. . , , .

private void Form1_Load(object sender, EventArgs e)
        {
            string[] Colors = new string[] { "red", "orange", "yellow", "green", "blue", "purple" };
            string[] Foods = new string[] { "fruit", "grain", "dairy", "meat", "sweet", "vegetable" };
            Colors = Shuffle(Colors);
            Foods = Shuffle(Foods);
            Pair[] meal = new Pair[Colors.Length - 1];
            for (int i = 0; i < Colors.Length - 1; i++)
            {
                meal[i] = new Pair(Colors[i],Foods[i]);
            }
        }
        static Random rdm = new Random();
        public string[] Shuffle(string[] c)
        {
            var random = rdm;
            for (int i = c.Length; i > 1; i--)
            {
                int iRdm = rdm.Next(i);
                string cTemp = c[iRdm];
                c[iRdm] = c[i - 1];
                c[i - 1] = cTemp;
            }
            return c;
        }
    }
    public class Pair
    {
         public string One;
         public string Two;
         public Pair(string m1, string m2)
         {
             One = m1;
             Two = m2;
         }
     }

- -

. , . , Fisher-Yates shuffle

+1

All Articles