Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Trying to create a for loop that gives all Unique combinations without using recursive process.

Trying to create a for loop that gives all Unique combinations without using recursive process.

Scheduled Pinned Locked Moved Algorithms
csharptutorialquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    Hi, no code, just an approach: if you were to use a ternary number representation (i.e. digits 0, 1, and 2; and digit weights that are powers of 3), then each combination could be written as a number, and incrementing such number would yield the next combination, so you would get 000, 001, 002, 010, 011, 012, 020, 021, 022, 100, ... 222 :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


    L Offline
    L Offline
    luke orun
    wrote on last edited by
    #3

    But if they have 15 schedules each then it wont be easy to solve it in that way or ?? Plus sometime the persons have different numbers of schedules.

    L 1 Reply Last reply
    0
    • L luke orun

      But if they have 15 schedules each then it wont be easy to solve it in that way or ?? Plus sometime the persons have different numbers of schedules.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #4

      the same principles apply: 1. if all persons have N schedules, use base=N notation and increment the number; 2. if all persons have different numbers of schedules, use an inhomogeneous base (each digit now has as weight the product of all numbers of schedules to the right of it, somewhat harder to implement) :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      1 Reply Last reply
      0
      • L luke orun

        I have 3 or more persons and they each have 3 or more schedules, how do i get all unique combination against each other. if you have 3 person and they all have 3 schedules each it should be 27 different combinations but how do i program this. If possible example code in VB.net or C#.net Example P = Person, S = Schedule: P1 S1 P2 S1 P3 S1 P1 S2 P2 S1 P3 S1 P1 S3 P2 S1 P3 S1 P1 S1 P2 S2 P3 S1 P1 S1 P2 S3 P3 S1 P1 S1 P2 S1 P3 S2 P1 S1 P2 S1 P3 S3 P1 S2 P2 S2 P3 S1 P1 S2 P2 S3 P3 S1 ETC ETC

        S Offline
        S Offline
        StarBP
        wrote on last edited by
        #5

        using System;
        using System.IO;

        namespace Schedule
        {
        class Program
        {
        static void Main(string[] args)
        {
        // Sample only.
        GetThreeSchedules("schedule3.txt", 7, 6, 10);
        GetFiveSchedules("schedule5.txt", 2, 3, 4, 1, 6);
        }
        public static void GetThreeSchedules(string filename, int one, int two, int three)
        {
        for (int a = 1; a <= one; a++)
        {
        for (int b = 1; b <= two; b++)
        {
        for (int c = 1; c <= three; c++)
        {
        File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + "\r\n");
        }
        }
        }
        }

            public static void GetFiveSchedules(string filename, int one, int two, int three, int four, int five)
            {
                for (int a = 1; a <= one; a++)
                {
                    for (int b = 1; b <= two; b++)
                    {
                        for (int c = 1; c <= three; c++)
                        {
                            for (int d = 1; d <= four; d++)
                            {
                                for (int e = 1; e <= five; e++)
                                {
                                    File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + " P4 S" + d + " P5 S" + e + "\\r\\n");
                                }
                            }
                        }
                    }
                }
            }
        }
        

        }

        Here is some code. I do not know how to generalize it further, though.

        L 1 Reply Last reply
        0
        • S StarBP

          using System;
          using System.IO;

          namespace Schedule
          {
          class Program
          {
          static void Main(string[] args)
          {
          // Sample only.
          GetThreeSchedules("schedule3.txt", 7, 6, 10);
          GetFiveSchedules("schedule5.txt", 2, 3, 4, 1, 6);
          }
          public static void GetThreeSchedules(string filename, int one, int two, int three)
          {
          for (int a = 1; a <= one; a++)
          {
          for (int b = 1; b <= two; b++)
          {
          for (int c = 1; c <= three; c++)
          {
          File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + "\r\n");
          }
          }
          }
          }

              public static void GetFiveSchedules(string filename, int one, int two, int three, int four, int five)
              {
                  for (int a = 1; a <= one; a++)
                  {
                      for (int b = 1; b <= two; b++)
                      {
                          for (int c = 1; c <= three; c++)
                          {
                              for (int d = 1; d <= four; d++)
                              {
                                  for (int e = 1; e <= five; e++)
                                  {
                                      File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + " P4 S" + d + " P5 S" + e + "\\r\\n");
                                  }
                              }
                          }
                      }
                  }
              }
          }
          

          }

          Here is some code. I do not know how to generalize it further, though.

          L Offline
          L Offline
          luke orun
          wrote on last edited by
          #6

          take this, if you have N persons and they all have a collection of schedules (0 .... N), how would you solve it dynamically so to speak. I mean your example works fine if it is a fixed nr of persons and schedules but thats not the case here. Person 1 has 3 schedules Person 2 has 2 schedules Person 3 has 4 schedules Person 4 has 3 schedules etc etc etc up to Person N using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Person person1 = new Person(3); Person person2 = new Person(2); Person person3 = new Person(4); Person person4 = new Person(3); // code - write to file all unique combinations } } class Person { public static int counter = 0; public int Id; public List<int> schedules = new List<int>(); public Person(int nrOfSchedules) { Id = counter++; // create schedules for person for (int i = 0; i < nrOfSchedules ; i++) { // think each object in the collection as an schedule id schedules.Add(i); } } } }

          L 1 Reply Last reply
          0
          • L luke orun

            take this, if you have N persons and they all have a collection of schedules (0 .... N), how would you solve it dynamically so to speak. I mean your example works fine if it is a fixed nr of persons and schedules but thats not the case here. Person 1 has 3 schedules Person 2 has 2 schedules Person 3 has 4 schedules Person 4 has 3 schedules etc etc etc up to Person N using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Person person1 = new Person(3); Person person2 = new Person(2); Person person3 = new Person(4); Person person4 = new Person(3); // code - write to file all unique combinations } } class Person { public static int counter = 0; public int Id; public List<int> schedules = new List<int>(); public Person(int nrOfSchedules) { Id = counter++; // create schedules for person for (int i = 0; i < nrOfSchedules ; i++) { // think each object in the collection as an schedule id schedules.Add(i); } } } }

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #7

            I already gave you most of the solution. the number of combinations N equals the product of the individual number of schedules. so you need a loop from 0 to N-1 :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            L 1 Reply Last reply
            0
            • L luke orun

              I have 3 or more persons and they each have 3 or more schedules, how do i get all unique combination against each other. if you have 3 person and they all have 3 schedules each it should be 27 different combinations but how do i program this. If possible example code in VB.net or C#.net Example P = Person, S = Schedule: P1 S1 P2 S1 P3 S1 P1 S2 P2 S1 P3 S1 P1 S3 P2 S1 P3 S1 P1 S1 P2 S2 P3 S1 P1 S1 P2 S3 P3 S1 P1 S1 P2 S1 P3 S2 P1 S1 P2 S1 P3 S3 P1 S2 P2 S2 P3 S1 P1 S2 P2 S3 P3 S1 ETC ETC

              B Offline
              B Offline
              Bassam Abdul Baki
              wrote on last edited by
              #8

              I needed something similar and came across [this](http://msdn.microsoft.com/en-us/library/aa302371.aspx\)[[^](http://msdn.microsoft.com/en-us/library/aa302371.aspx\)]. However, at 13 elements or more, it fails due to 13!. I still have not found anything perfect.

              Web - Blog - RSS - Math - BM

              1 Reply Last reply
              0
              • L Luc Pattyn

                I already gave you most of the solution. the number of combinations N equals the product of the individual number of schedules. so you need a loop from 0 to N-1 :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                L Offline
                L Offline
                luke orun
                wrote on last edited by
                #9

                I understand the solution but i've worked with it for a couple of days now and i can't implement it to code.

                L 1 Reply Last reply
                0
                • L luke orun

                  I understand the solution but i've worked with it for a couple of days now and i can't implement it to code.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  Hi, something along these lines:

                  // example: 3 persons, with 4/2/3 schedules
                  int persons=3;
                  int[] max=new int[] {4,2,3};
                  int prod=1;
                  for (int p=0; p< persons; p++) prod*=max[p];
                  for (int comb=0; comb< prod; comb++) {
                  int val=comb;
                  string s="";
                  for (int p=persons-1; p>=0; p--) {
                  int maxp=max[p];
                  int mod=val%maxp;
                  s=mod.ToString()+" "+s;
                  val=val/maxp;
                  }
                  Console.WriteLine(s);
                  }

                  :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  modified on Monday, May 11, 2009 8:57 AM

                  L 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, something along these lines:

                    // example: 3 persons, with 4/2/3 schedules
                    int persons=3;
                    int[] max=new int[] {4,2,3};
                    int prod=1;
                    for (int p=0; p< persons; p++) prod*=max[p];
                    for (int comb=0; comb< prod; comb++) {
                    int val=comb;
                    string s="";
                    for (int p=persons-1; p>=0; p--) {
                    int maxp=max[p];
                    int mod=val%maxp;
                    s=mod.ToString()+" "+s;
                    val=val/maxp;
                    }
                    Console.WriteLine(s);
                    }

                    :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    modified on Monday, May 11, 2009 8:57 AM

                    L Offline
                    L Offline
                    luke orun
                    wrote on last edited by
                    #11

                    Thanks for the example but i cant run the code. i think something is wrong on you first for-loop.

                    L 1 Reply Last reply
                    0
                    • L luke orun

                      Thanks for the example but i cant run the code. i think something is wrong on you first for-loop.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      HTML monster in action again. fixed. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      L 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        HTML monster in action again. fixed. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                        L Offline
                        L Offline
                        luke orun
                        wrote on last edited by
                        #13

                        Thank you it worked fine :-D

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups