Hi...
I have question on how to do the combination in Window Form.
Now, I have a list of data
eg.

1. 1111 1112 1113 1114 1134 1133 1222 1345 1444
2. 1111 1122 1134 1135 1145 1234 1235 1245 1445
I have already split them in array and i already find their key and value.
NOw i need to do the combination...?
So who can help me......??
Thanks a lot..

Recommended Answers

All 10 Replies

You need to explain more about what you mean by combination? Do you want every item in list 1 combined with an item in list 2?

You need to explain more about what you mean by combination? Do you want every item in list 1 combined with an item in list 2?

no no....
I give a simple example..
1. 1 2 3
2. 1 2 3 4
3. 1 3
4. 1 2 3 4 5

now i want combination like this

{1,2},{1,3},{1,4}.....
{1,2,3}, {1,2,4}....
And next..
I will count the key and value
eg.

{1,2} - 3
{1,3} - 4
and so on..
{1,2} - 3 mean that the combination{1,2} appear 3 times .

So now, I need to do the combination in any length of data in string array...

You need to explain more about what you mean by combination? Do you want every item in list 1 combined with an item in list 2?

no no....
I give a simple example..
1. 1 2 3
2. 1 2 3 4
3. 1 3
4. 1 2 3 4 5

now i want combination like this

{1,2},{1,3},{1,4}.....
{1,2,3}, {1,2,4}....
And next..
I will count the key and value
eg.

{1,2} - 3
{1,3} - 4
and so on..
{1,2} - 3 mean that the combination{1,2} appear 3 times .

So now, I need to do the combination in any length of data in string array...

Sure mate you must be looking for some sort of an algorithm. I do not know what exactly are you looking for but you can always archieve alot with loops. If you exaplain your problem with human english not computer science language. I'll be glad to help you out

It's not as easy as it looks to write a generic combination program. Attached you'll find a DLL with one in it. Here is a sample program using it:

using System;
using System.Text;
using Whittle;

namespace ComboTest {
    class Program {
        static void Main(string[] args) {
            String[] str = { "1", "2", "3", "4", "5" };

            for (int i = 1; i <= str.Length; i++) {
                Console.WriteLine("{0} items taken {1} at a time results in:", str.Length, i);
                Combinations<String> c = new Combinations<string>(str, i);
                foreach (String[] s in c) {
                    Console.Write("{0} ", FormatString(s));
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            Console.ReadLine();
        }

        static String FormatString(String[] input) {
            StringBuilder sb = new StringBuilder();
            foreach (String s in input) {
                if (sb.Length == 0) {
                    sb.Append("{ ");
                } else {
                    sb.Append(", ");
                }
                sb.Append(s.ToString());
            }
            sb.Append(" }");

            return sb.ToString();
        }
    }
}

This generates this output:

5 items taken 1 at a time results in:
{ 1 } { 2 } { 3 } { 4 } { 5 }

5 items taken 2 at a time results in:
{ 1, 2 } { 1, 3 } { 1, 4 } { 1, 5 } { 2, 3 } { 2, 4 } { 2, 5 } { 3, 4 } { 3, 5 } { 4, 5 }

5 items taken 3 at a time results in:
{ 1, 2, 3 } { 1, 2, 4 } { 1, 2, 5 } { 1, 3, 4 } { 1, 3, 5 } { 1, 4, 5 } { 2, 3, 4 } { 2, 3, 5 } { 2, 4, 5 } { 3, 4, 5 }

5 items taken 4 at a time results in:
{ 1, 2, 3, 4 } { 1, 2, 3, 5 } { 1, 2, 4, 5 } { 1, 3, 4, 5 } { 2, 3, 4, 5 }

5 items taken 5 at a time results in:
{ 1, 2, 3, 4, 5 }

To use the class you create a Combination object indicating the type that you want combinations of, passing an array and how many you want in each group. For example, if we had an int array called bob, and we wanted them 3 at a time we'd use Combinations<int> myCombos = new Combinations<int>(bob, 3) I'm not ready to release the source to this right now, it needs some work on readability and it's not quite fully generic (it uses IEnumerable rather than IEnumberable<T>, etc.)

no no....
I give a simple example..

I don't understand anything of all your number rubbish.
Could you give a clear and concise example?

How many numbers are there?
How many of them are used?
Is the order of the numbers important?
Can you repeat a number?

It's not as easy as it looks to write a generic combination program. Attached you'll find a DLL with one in it. Here is a sample program using it:

using System;
using System.Text;
using Whittle;

namespace ComboTest {
    class Program {
        static void Main(string[] args) {
            String[] str = { "1", "2", "3", "4", "5" };

            for (int i = 1; i <= str.Length; i++) {
                Console.WriteLine("{0} items taken {1} at a time results in:", str.Length, i);
                Combinations<String> c = new Combinations<string>(str, i);
                foreach (String[] s in c) {
                    Console.Write("{0} ", FormatString(s));
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            Console.ReadLine();
        }

        static String FormatString(String[] input) {
            StringBuilder sb = new StringBuilder();
            foreach (String s in input) {
                if (sb.Length == 0) {
                    sb.Append("{ ");
                } else {
                    sb.Append(", ");
                }
                sb.Append(s.ToString());
            }
            sb.Append(" }");

            return sb.ToString();
        }
    }
}

This generates this output:

5 items taken 1 at a time results in:
{ 1 } { 2 } { 3 } { 4 } { 5 }

5 items taken 2 at a time results in:
{ 1, 2 } { 1, 3 } { 1, 4 } { 1, 5 } { 2, 3 } { 2, 4 } { 2, 5 } { 3, 4 } { 3, 5 } { 4, 5 }

5 items taken 3 at a time results in:
{ 1, 2, 3 } { 1, 2, 4 } { 1, 2, 5 } { 1, 3, 4 } { 1, 3, 5 } { 1, 4, 5 } { 2, 3, 4 } { 2, 3, 5 } { 2, 4, 5 } { 3, 4, 5 }

5 items taken 4 at a time results in:
{ 1, 2, 3, 4 } { 1, 2, 3, 5 } { 1, 2, 4, 5 } { 1, 3, 4, 5 } { 2, 3, 4, 5 }

5 items taken 5 at a time results in:
{ 1, 2, 3, 4, 5 }

To use the class you create a Combination object indicating the type that you want combinations of, passing an array and how many you want in each group. For example, if we had an int array called bob, and we wanted them 3 at a time we'd use Combinations<int> myCombos = new Combinations<int>(bob, 3) I'm not ready to release the source to this right now, it needs some work on readability and it's not quite fully generic (it uses IEnumerable rather than IEnumberable<T>, etc.)

How about if i don't want to mention how many in each group? because i need to print out result straight away...can help?

You have a strange way of thinking about combinations, but perhaps I'm getting old.
To me combinations is all about a number of n "things" and on how many ways you can combine them by taking out p of those "things". See Momeraths example.

If you can't figure out how to prevent the one line from printing out then maybe this project is beyond your capabilities.

It's not as easy as it looks to write a generic combination program. Attached you'll find a DLL with one in it. Here is a sample program using it:

using System;
using System.Text;
using Whittle;

namespace ComboTest {
    class Program {
        static void Main(string[] args) {
            String[] str = { "1", "2", "3", "4", "5" };

            for (int i = 1; i <= str.Length; i++) {
                Console.WriteLine("{0} items taken {1} at a time results in:", str.Length, i);
                Combinations<String> c = new Combinations<string>(str, i);
                foreach (String[] s in c) {
                    Console.Write("{0} ", FormatString(s));
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            Console.ReadLine();
        }

        static String FormatString(String[] input) {
            StringBuilder sb = new StringBuilder();
            foreach (String s in input) {
                if (sb.Length == 0) {
                    sb.Append("{ ");
                } else {
                    sb.Append(", ");
                }
                sb.Append(s.ToString());
            }
            sb.Append(" }");

            return sb.ToString();
        }
    }
}

This generates this output:

5 items taken 1 at a time results in:
{ 1 } { 2 } { 3 } { 4 } { 5 }

5 items taken 2 at a time results in:
{ 1, 2 } { 1, 3 } { 1, 4 } { 1, 5 } { 2, 3 } { 2, 4 } { 2, 5 } { 3, 4 } { 3, 5 } { 4, 5 }

5 items taken 3 at a time results in:
{ 1, 2, 3 } { 1, 2, 4 } { 1, 2, 5 } { 1, 3, 4 } { 1, 3, 5 } { 1, 4, 5 } { 2, 3, 4 } { 2, 3, 5 } { 2, 4, 5 } { 3, 4, 5 }

5 items taken 4 at a time results in:
{ 1, 2, 3, 4 } { 1, 2, 3, 5 } { 1, 2, 4, 5 } { 1, 3, 4, 5 } { 2, 3, 4, 5 }

5 items taken 5 at a time results in:
{ 1, 2, 3, 4, 5 }

To use the class you create a Combination object indicating the type that you want combinations of, passing an array and how many you want in each group. For example, if we had an int array called bob, and we wanted them 3 at a time we'd use Combinations<int> myCombos = new Combinations<int>(bob, 3) I'm not ready to release the source to this right now, it needs some work on readability and it's not quite fully generic (it uses IEnumerable rather than IEnumberable<T>, etc.)

Can u show the combination class to me?...because i can't open your attached zip file. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.