Finding the size of multiple lists.

This is a discussion on Finding the size of multiple lists. within the C# Programming forums, part of the General Programming Boards category; Hi, I am writing a Windows Form program, and currently it has 3 Lists. I need to loop through all ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Finding the size of multiple lists.

    Hi,

    I am writing a Windows Form program, and currently it has 3 Lists. I need to loop through all three, and use each possible combination, so am using three nested for loops.

    I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off, as I need to save all the possible combinations of list a + b + c.

    This is what I wrote:

    Code:
            public int CalculateOrderToProcessLists()//for three lists
            {
                int order = 0;
    
                if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() > sListThree.Count())
                {
                    order = 123;
                }
                else if(sListOne.Count() > sListTwo.Count() && sListTwo.Count() < sListThree.Count() && sListOne.Count() > sListThree.Count())
                {
                    order = 132;
                }
                else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() > sListThree.Count() && sListOne.Count() > sListThree.Count())
                {
                    order = 213;
                }
                else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() > sListThree.Count() && sListOne.Count() < sListThree.Count())
                {
                    order = 231;
                }
                else if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() < sListThree.Count() && sListOne.Count() < sListThree.Count())
                {
                    order = 312;
                }
                else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() < sListThree.Count())
                {
                    order = 321;
                }
                else
                {
                    order = 999;
                }
                return order;
            }
    My problem is I would like to be able to have 4 (or more if possible) lists, and to calculate what order to process the lists will be exponentially more complex.

    I was wondering if anyone could help me come up with a better way of comparing the sizes of the lists, so that I can use the nested loops of say 4 lists, but being able to use N number of lists would be awesome.

    Thanks for any help, it's most appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Swerve View Post
    I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off,
    I do not really understand what you mean by this. Looks like bug that you have to fix instead of some tricky workaround you are trying to do with finding longest list and looping through it first.

    Show the small code example that illustrates the problem
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Quote Originally Posted by Swerve View Post
    as I need to save all the possible combinations of list a + b + c.
    Generating all possible combinations arrays is a known problem and fairly documented in the web. For instance: Print all possible combinations of r elements in a given array of size n. In a nutshell you will move all your lists into a single array (you don't need to calculate order of execution) and find all possible permutations from there.

    Solutions like the above, use recursion. A generic algorithm should be something like this:

    Code:
    // for an array or set P(a1...aN)
    
    permute(i) 
       if i == N  output A[N] and end
       else 
          for j = i to N do 
             swap(A[i], A[j]) 
             permute(i+1) 
             swap(A[i], A[j])
    Other solutions don't use recursion, which makes them dependent on the number and size of the sets to be processed. But...


    Quote Originally Posted by Swerve View Post
    but being able to use N number of lists would be awesome.
    ... because of this, a recursive solution is the best option. Even if you can't afford to move all the lists elements into a single array and end up choosing other type of non recursive solutions (google "find all combination in a jagged array", for instance), you will still need to convert that solution into a recursive function in order to handle the fact you don't know the vertical dimension of your jagged array until runtime.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding LinkedList Size
    By johngoodman in forum C Programming
    Replies: 7
    Last Post: 02-17-2013, 01:55 PM
  2. Finding the file size.
    By +Azazel+ in forum C Programming
    Replies: 18
    Last Post: 10-16-2007, 10:21 AM
  3. Finding the size of the borders.
    By jpfarias in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 09:50 PM
  4. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 03:51 PM
  5. Finding the size of a directory
    By J_Bravo in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2002, 08:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21