none
Combination of Elements with by Using Array RRS feed

  • Question

  • Goal:
    Make a combination of the element by using the input data. The goal is to achieve the output.

    An example      
    
    Current situation (input):      
    jim west    aaa 12345
    Le tomo     bbb 
    Sara brown      
    
    Requested Result(output):       
    jim west    aaa 12345
    Le tomo     aaa 12345
    Sara brown  aaa 12345
    jim west    bbb 
    Le tomo     bbb 
    Sara brown  bbb 
    
    -------------------------------
    
    Another example     
    
    Current situation (input):      
    jim west    aaa 12345
    Le tomo     bbb 32154
    Sara brown  ccc 78946
    
    Requested Result(output):       
    jim west    aaa 12345
    Le tomo     aaa 12345
    Sara brown  aaa 12345
    jim west    bbb 32154
    Le tomo     bbb 32154
    Sara brown  bbb 32154
    jim west    ccc 78946
    Le tomo     ccc 78946
    Sara brown  ccc 78946
    
    -------------------------------
    
    Another example
    
    Current situation (input):              
    jim west    aaa 12345
    Le tomo     
    Sara brown      
    
    Requested Result(output):       
    jim west    aaa 12345
    Le tomo     aaa 12345
    Sara brown  aaa 12345

    Problem:
    *I don't know how to solve it. Do you have any advice about what path I should use?

    Info:
    *I was unable to use permutation in this context.

    public class Program
    {
        static void Main(string[] args)
        {
    
            string[] column1 = { "jim west", "Le tomo", "Sara brown" };
            string[] column2 = { "aaa", "bbb"};
            string[] column3 = { "12345" };
    
    
    
        }
    }

    Tuesday, February 4, 2020 12:09 PM

All replies

  • It is unclear from your examples but do you want all combinations of inputs or just a subset? To get a combination of all inputs you'll need a series of nested for loops that enumerate each array and combine it with the elements of the other arrays. Something like this, not tested:

    foreach value1 in column1
       foreach value2 in column2
          foreach value3 in column3
              combination = String.Join(" ", value1, value2, value3);
    That would combine all the values (assuming you want fixed order). You just need to decide what to do with the combined value each time through the loop.



    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, February 4, 2020 2:57 PM
    Moderator
  • You are trying to specify what needs to be done using examples. I think that if you can specify what needs to be done using words then you will understand better what needs to be done and will be much more likely to figure it out yourself.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 4, 2020 6:11 PM
  • Hi Sakura Data,
    According to your three examples, you can put "aaa 12345" "bbb 32154" "ccc 78946" in an array and then use a for loop.
    Here is a simple you can refer to.

    string[] column1 = { "jim west", "Le tomo", "Sara brown" };
    string[] column2 = { " aaa 12345"," bbb 34567", " ccc 78946" };
    for (int i=0; i<column1.Length;i++) {
        for (int j = 0; j < column2.Length; j++) {
            Console.WriteLine(column1[i]+column2[j]); 
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 5, 2020 7:41 AM
  • I had sinus surgery today and I'm loopy as a dingbat running a hamster wheel in a hula-hoop. If anybody's getting replies via email, it just occurred to me now what I'm doing by deleting bad-looking or insufficiently-commented stuff to repost entirely. SORRY!

    using System;
    
    namespace ConsoleApplication
    {
        class Program
        {
            // The combinatoric function
            static string Combinatoric(string[][] table)
            {
                string r = string.Empty;
    
                // Loop through the multidimensional array.  Each array element here is itself another array.
                // This passes once through each sub-dimensional array in order get letters and numbers
                for (Int32 di0 = 0; di0 < table.Length; di0++)
                {
                    string[] thisrow = table[di0];
    
                    // Where this one contains only a name, we can skip it
                    // We only want to secondary-scan where the currently selected row has more than just a name
                    if (thisrow.Length > 1)
                    {
                        // iterate the parent table again for name selection
                        for (int di1 = 0; di1 < table.Length; di1++)
                        {
                            // Get only the first element from each column, ie the name field
                            r += table[di1][0];
    
                            // Append letters if existing
                            if (thisrow.Length > 1)
                            {
                                r += @" ";
                                r += thisrow[1];
                            }
    
                            // Append numbers if they exist
                            if (thisrow.Length > 2)
                            {
                                r += @" ";
                                r += thisrow[2];
                            }
    
                            // Where we're currently still INSIDE the combo, meaning we're going to generate another
                            // line of r, add a text-newline to the end of the string.  Do not add a newline 
                            // if the current row-column index is the last one to process.
                            if (di1 < (table.Length - 1))
                            {
                                r += System.Environment.NewLine;
                            }
                        }
                    }
    
                    // Where we're currently still INSIDE the table, meaning we're going to generate another
                    // line of r, add a text-newline to the end of the string.  Do not add a newline 
                    // if the current table-row index is the last one to process.
                    if (di0 < (table.Length - 1))
                    {
                        r += System.Environment.NewLine;
                    }
                }
    
                return r;
            }
    
    
            // The main application entrypoint
            static int Main(string[] args)
            {
                int r = 0;
    
                // A multidimensional array - this one is "jagged"
                //   The array table[] contains 3 arrays itself, each of which 
                //   should be considered a single table row
                //
                //   Each "dimension" is another distinct array inside the table array.
                //     * Each of these elements of table should be considered a data row
                //
                //   However many entries each data row contains, each entry should considered a single column or 
                //   field in that row
                //
                // So this is a database table
                string[][] table = new string[3][];
    
                Console.WriteLine();
                Console.WriteLine(@"Using your first example:");
                // Row 0 has 3 columns
                table[0] = new string[] { @"jim west", @"aaa", @"12345" };
                // Row 1 has 2 columns
                table[1] = new string[] { @"Le tomo", @"bbb" };
                // Row 2 has 1 column
                table[2] = new string[] { @"Sara brown" };
    
                Console.WriteLine(Combinatoric(table));
                Console.WriteLine();
                Console.WriteLine(@"Press any key to continue");
                Console.WriteLine();
                Console.ReadKey();
    
                Console.WriteLine(@"Second Example:");
                // Row 0 has 3 columns
                table[0] = new string[] { @"jim west", @"aaa", @"12345" };
                // Row 1 has 3 columns
                table[1] = new string[] { @"Le tomo", @"bbb", @"32154" };
                // Row 2 has 3 columns
                table[2] = new string[] { @"Sara brown", @"ccc", @"78946" };
                Console.WriteLine(Combinatoric(table));
                Console.WriteLine();
                Console.WriteLine(@"Press any key to continue");
                Console.WriteLine();
                Console.ReadKey();
    
                Console.WriteLine(@"Third Example:");
                // Row 0 has 3 columns
                table[0] = new string[] { @"jim west", @"aaa", @"12345" };
                // Row 1 has 1 column
                table[1] = new string[] { @"Le tomo" };
                // Row 2 has 1 column
                table[2] = new string[] { @"Sara brown" };
                Console.WriteLine(Combinatoric(table));
    
                Console.WriteLine();
                Console.WriteLine(@"Press any key to exit");
                Console.WriteLine();
                Console.ReadKey();
    
                return r;
            }
        }
    }





    Wednesday, February 5, 2020 8:36 AM
  • All you people that are giving complete solutions: note that this is obviously a class assignment and the student needs to learn. If they turn in a complete solution you provide without understanding how it works then it really is not an answer. The question is "Do you have any advice about what path I should use" so even the student is requesting guidance.


    Sam Hobbs
    SimpleSamples.Info


    Wednesday, February 5, 2020 3:16 PM
  • All you people that are giving complete solutions: note that this is obviously a class assignment and the student needs to learn. If they turn in a complete solution you provide without understanding how it works then it really is not an answer. The question is "Do you have any advice about what path I should use" so even the student is requesting guidance.


    Sam Hobbs
    SimpleSamples.Info


    I had commented on this myself in an earlier/deleted post.

    At the end of the day the onus is on the student OP whether to straight up plagiarize code from MSDN which his professor will clearly see is written beyond the scope of the OP's skill or training up to now, or whether to look at the many solutions and step through them with a VS Debugger and make a legitimate effort to understand what everybody's various logic and logical implementation in code is about so to write their own version in their own style.

    Look around MSDN too.  Back around 2010 people were hardcore responsible about not providing code or even a lot of help to people who wanted a low-level keystroke logger such as provided by LLKHF.  That's because the likelihood of legitimate use-cases from people who have to ask how to use it is slim to none.  By 2018 this stuff was common place on MSDN forums... sometimes as much as daily but at least weekly.

    So I did somebody's homework without regard for the fact that they might plagiarize it, get caught, and suffer consequences.  Individually.

    Other folks are leaving loaded guns laying around public parks where children play.  The potential for mass consequences is real and probable.

    Wednesday, February 5, 2020 4:40 PM
  • Something else I did not make clear is that it is often more work to explain things than it is to write the code but an explanation is the better response for students.

    Sakura asked for advice but the requirements are unclear. I often see discussions erupt into long discussions when the question is not clear so I was waiting for clarification.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, February 5, 2020 11:37 PM
  • Something else I did not make clear is that it is often more work to explain things than it is to write the code but an explanation is the better response for students.

    Sakura asked for advice but the requirements are unclear. I often see discussions erupt into long discussions when the question is not clear so I was waiting for clarification.



    Sam Hobbs
    SimpleSamples.Info

    So in the words of an avid FPS player:  "The rest of us are all kill-stealing fucktards?"
    Thursday, February 6, 2020 6:16 AM
  • Hi Sakura Data,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 4, 2020 8:54 AM