1

I have 20 pots that are used to create a schedule of 2 pot groupings. I need to find all possible schedules that can be created.

// create range of pots
 IEnumerable<int> pots = Enumerable.Range(1, 20);

Get all the possible 2 pot groups:

    public DataTable CreateCombinations(IEnumerable<int> pots)
    {

        DataTable testTable = new DataTable();
        testTable.Columns.Add("Pot1");
        testTable.Columns.Add("Pot2");
        for (int i = 0; i < pots.Count(); i++)
        {
            for (int j = i + 1; j < pots.Count(); j++)
            {
                var row = testTable.NewRow();
                row["Pot1"] = pots.ElementAt(i);
                row["Pot2"] = pots.ElementAt(j);
                testTable.Rows.Add(row);
            }
        }

        return testTable;
    }

List of all the possible schedules that can be created from the 1,n combinations:

public DataTable CreatePossibleSchedules(DataTable dt)
        {
            DataTable testTable = new DataTable();
            testTable.Columns.Add("Pot1");
            testTable.Columns.Add("Pot2");
            testTable.Columns.Add("Schedule");
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                if(dt.Rows[i]["Pot1"].ToString() == "1")
                {
                    for (var x = i; x < dt.Rows.Count; x++)
                    {
                        string pot1 = dt.Rows[x]["Pot1"].ToString();
                        string pot2 = dt.Rows[x]["Pot2"].ToString();
                        string schedule = (i + 1).ToString();
                        bool exists = testTable.AsEnumerable().Any(row => 
(pot1 == row.Field<string>("Pot1") || pot1 == row.Field<string>("Pot2") || 
pot2 == row.Field<string>("Pot1") || pot2 == row.Field<string>("Pot2")) && 
schedule == row.Field<string>("Schedule"));
                        if (!exists)
                        {
                            var row = testTable.NewRow();
                            row["Pot1"] = dt.Rows[x]["Pot1"];
                            row["Pot2"] = dt.Rows[x]["Pot2"];
                            row["Schedule"] = i + 1;
                            testTable.Rows.Add(row);
                        }
                    }
                }                
            }

            return testTable;
        }

This gives me the possible schedules for 1,2 through 1,20:
Schedule 1:
1,2
3,4
...
19,20
Down to Schedule 20:
1, 20
2, 3
18, 19

What I don't know how to do is start over to start getting the rest of the possible schedules:
Schedule 21:
1, 2
3, 5
4, 6

Hope this makes sense, not sure if I've explained the problem very well.

tcrafton
  • 97
  • 2
  • 10
  • We agree that the order doesn't matter ? I.e, you don't need to take 2,1 as a different possiblity from 1,2 ? – Pac0 Sep 26 '18 at 15:27
  • Correct, 1,2 is the same as 2,1 – tcrafton Sep 26 '18 at 15:29
  • 1
    Thanks to this question and the duplicate targets, I now have another cool blog articles from Eric Lippert to read :D . – Pac0 Sep 26 '18 at 15:30
  • 1
    read carefully the duplicate targets provided as close reason. (it might take some time to understand all the implications) . If you still feel it doesn't answer the question (however, I think they do), try to carefully explain in which way they don't with an edit on your post, it might be reopened then. – Pac0 Sep 26 '18 at 15:32
  • Will do, thank you. – tcrafton Sep 26 '18 at 15:33
  • It's not clear to me that this is a duplicate; this doesn't appear to be a Cartesian product at first glance. Let me make sure I understand the question. You want all the permutations of the sequence 1, … 20, but with the restrictions that the sequence is taken in pairs, that the first number of each pair is not decreasing with respect to other pairs, and that the second number in each pair is larger than the first, yes? – Eric Lippert Sep 26 '18 at 16:07
  • Yes, permutations of the sequence 1 ...20 taken in pairs. A schedule is created by splitting the sequence into pairs, I need to find out all of the possible schedules that can be created. – tcrafton Sep 26 '18 at 17:07

0 Answers0