|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Algorithm Design
Hi
My maths is really rusty. I'm wondering if you guys could give me a hand with an algorithm. I need to write a algorithm for generating combinations from n number of sets. I can write code that will take my fixed number of sets and produce the combinations but Im lost on how I would be able to do this for n number of sets. Here is my existing fixed number of sets combination generator: int[] firstSet = new int[] { 1, 2, 3, 4, 5 }; int[] secondSet = new int[] { 6, 7, 8, 9, 10, 11 }; int[] thirdSet = new int[] { 12, 13, 14 }; int[] fourthSet = new int[] { 15, 16, 17, 18 }; int counter = 0; foreach (int firstSetVariable in firstSet) { foreach (int secondSetVariable in secondSet) { foreach (int thirdSetVariable in thirdSet) { foreach (int fourthSetVariable in fourthSet) { Response.Write(counter + ": " + firstSetVariable + "," + secondSetVariable + "," + thirdSetVariable + "," + fourthSetVariable + "<br />"); counter += 1; } } } } Will appreciate anyones help or guidance. Thanks |
|
#2
|
|||||
|
|||||
|
This naturally suggests a recursive solution.
Here's one way in Python using generators: python Code:
(There is no math involved, by the way.) |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Algorithm Design |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|