Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to pass a number as input and to get a list of string arrays(in specific format) as output.

For example:

If input is 4, then the output should be

[{ "0", "1", "2", "3", "4"},
{ "0", "1", "2", "3&4"},
{ "0", "1", "2&3&4"},
{ "0", "1&2", 3", 4"},
{ "0", "1", "2&3", "4"},
{ "0", "1&2&3", "4"},
{ "0", "1&2&3&4"},
{ "0&1", "2&3", "4"},
{ "0&1&2", "3&4"},
{ "0&1", "2&3&4"},
{ "0&1", "2", "3&4"},
{ "0&1", "2&3&4"},
{ "0&1&2&3&4"}]


In all the combinations the values are in order(0 to input value) and have been present only once.

Can anyone help me with any ideas?

Thanks in Advance.

What I have tried:

C#
public static List<string[]> GetAllCombinations()
        {
            List<int> num = new List<int>() { 0, 1, 2, 3, 4 };
            List<string[]> strArr = new List<string[]>();
            int k = 1;
            while (k <= num.Count)
            {
                List<string> st1 = new List<string>();
                for (int i = 0; i < num.Count; i = i + k)
                {
                    string value = "";
                    for (int j = i; (j < num.Count && j < (i + k)); j++)
                    {
                        if (value != "")
                            value = value + "&";
                        value = value + num[j];
                    }
                    st1.Add(value);
                }
                strArr.Add(st1.ToArray());
                k++;
            }
            return strArr;
        }


Its giving most of the combinations but not the middle combinations.
Posted
Updated 1-Jul-20 9:29am
v10
Comments
MadMyche 1-Jul-20 7:37am    
Show what you actually tried; and choose ONE language, right now it looks like SPAM with all of them tags
Member 11484114 1-Jul-20 10:16am    
Hi,
I have edited the question and pasted the code that I have tried. But its giving only the partial output. BTW, I have removed multiple tags. Thanks for it.
Richard MacCutchan 1-Jul-20 9:33am    
You have tagged this with multiple languages. Which one are you planning to use?
Member 11484114 1-Jul-20 10:21am    
I am planning to use it in C#. Even its in C or python also its fine. I can convert it to C#.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 
Comments
Member 11484114 1-Jul-20 10:20am    
Hi,
I have edited the question and pasted the code that I have tried which is giving only the partial output. Do u know what I am missing?
If I gave you (I won't!!!) a magic function that can do it for the numbers [0,N-1], would you be able to get the results for [0,N]? Think very hard, then do exactly that.
 
Share this answer
 
First of all your sample have 13 output strings where there should be 16.
[{ "0", "1", "2", "3", "4"},
{ "0", "1", "2", "3&4"},
{ "0", "1", "2&3", "4"},
{ "0", "1", "2&3&4"},
{ "0", "1&2", "3", "4"},
{ "0", "1&2", "3&4"}, missing
{ "0", "1&2&3", "4"},
{ "0", "1&2&3&4"},
{ "0&1", "2", "3", "4"}, missing
{ "0&1", "2", "3&4"},
{ "0&1", "2&3", "4"},
{ "0&1", "2&3&4"}, this one is repeated 2 times in your sample output
{ "0&1&2", "3", "4"}, missing
{ "0&1&2", "3&4"},
{ "0&1&2&3", "4"}, missing
{ "0&1&2&3&4"}]


Quote:
If input is 4, then the output should be

You didn't gave any rule for the 4 missing strings in result and the duplicate.

As programmer, your first job is to make sure requirement and samples make sense and are consistent.
Then you need to make some logic, an algorithm, to solve the problem.
For each string, you get n numbers/digits and n-1 separators, each separator have 2 possibilities. Enumerating all combination of the separators match values of a binary counter.
 
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