Trying to create a for loop that gives all Unique combinations without using recursive process.
-
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
-
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.
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
-
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
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.
-
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.
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); } } } }
-
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); } } } }
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
-
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
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.
-
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
-
I understand the solution but i've worked with it for a couple of days now and i can't implement it to code.
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
-
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
-
Thanks for the example but i cant run the code. i think something is wrong on you first for-loop.
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.
-
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.