Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Given an numbers ... E.g. {1,2,3,4}
I need a way to generale all possible combinations and subset of the numbers.
I need to have all subsets (all subsets of 1 element, all subset of 2 elements, all subset of n elements) an of each subset all possible permutations.
I don't have idea
ALL combination!
Is there a quick way?
For example result should be:
1
1 2
1 3
1 4
1 2 3
1 2 4
1 3 4
1 2 3 4
2
2 3
2 4
2 3 4
3
3 4
4


What I have tried:

This my code

C#
for (int i = 0; i < (2^4)-1; i++)
            {
                for (int j = 0; j < i+1; j++)
                {
                    for (int k = j; k < i; k++)
                    {
                        Console.Write(k+1);
                    }
                    Console.WriteLine();
                }
            }


this my output 
1
1 2
2
1 2 3
2 3
3
1 2 3 4
2 3 4
3 4
4




NOTE : This my question in Stackoverflow and not solution Because they think it is duplicate
Posted
Updated 16-Jul-19 16:00pm
v2
Comments
F-ES Sitecore 16-Jul-19 11:39am    
It is a duplicate, it is one of the most frequently asked homework questions there is, I'm sure you can find the answer if you google. Just remember that everyone else in the class who is cheating will probably be handing in the same code as you.
HelloIt'sMe_M 16-Jul-19 11:43am    
no this is problem not homework
i google it but not found any helpful solution
F-ES Sitecore 16-Jul-19 11:54am    
That's neither here nor there :) you'll still find a solution if you google.
Dominic Burford 16-Jul-19 11:45am    
Looks like homework to me too.

There is a very simple way: Permutations, Combinations, and Variations using C# Generics[^]

But ... hand in a solution based on that, and you'll probably get kicked off the course for plagiarism ...
 
Share this answer
 
First of all, your requirement example is wrong because 'combinations' and 'permutations' also mean out of order.
This means that 1324, 2341, 4321 ... are solutions too.
Quote:
Is there a quick way?

Simpler than your code (assuming it is OK) : NO.

As you can see, your output is missing all results with a hole 124, 134, 13, 24 ...
You have to analyze which result is missing, understand why, and refine your code.
The debugger can help you.

I fear a complete rewrite is in order.

[Update]
Hint for a possible Solution:
- make a loop that pick 1 digit at the time
- inside the loop print actual 1 digit number
- inside that loop, make a second loop that pick a second digit
- make sure it is not the same as first digit
- print number made of first and second digits
...
 
Share this answer
 
v2
Comments
Rick York 16-Jul-19 12:55pm    
A rewrite of about a dozen lines shouldn't be that big of a deal. :)
Patrice T 16-Jul-19 13:51pm    
I know, but may be a little complicated for a newbie.
Richard Deeming 16-Jul-19 13:41pm    
Out-of-order only applies to permutations, not combinations. :)
Patrice T 16-Jul-19 13:50pm    
Since both are in requirement, I guess my solution is still ok :)
"Combinations" or "permutations"? They're different beasts. Your example output looks more like combinations.

In either case, Eric Lippert has a good series of blog posts explaining how they work and how to generate them:
Producing permutations, part one | Fabulous adventures in coding[^]
Producing combinations, part one | Fabulous adventures in coding[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900