Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. find all possible combinations in a List < List < int > > [modified]

find all possible combinations in a List < List < int > > [modified]

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cechode
    wrote on last edited by
    #1

    can't figure out a lambada expression that would yield a falttened result of all combinations within the input list of lists

    List < List < int > > FindEm(List < List < int > > inputLists) {
    var X = ?!?
    }

    assuming that the input was populated with

            List A = new List(new int\[\] { 1, 2, 3 });
            List A = new List(new int\[\] { 4, 5, 6 });
            List A = new List(new int\[\] { 7, 8, 9 });
    
            List\> lists = new List\>( );
            lists.Add(A);
            lists.Add(B);
            lists.Add(C);
    

    the result would look something like 1,4,7 1,4,8 1,4,9 2,4,7 ..... any help or direction would be greatly appriciated :)

    modified on Monday, October 5, 2009 1:35 PM

    L 1 Reply Last reply
    0
    • C cechode

      can't figure out a lambada expression that would yield a falttened result of all combinations within the input list of lists

      List < List < int > > FindEm(List < List < int > > inputLists) {
      var X = ?!?
      }

      assuming that the input was populated with

              List A = new List(new int\[\] { 1, 2, 3 });
              List A = new List(new int\[\] { 4, 5, 6 });
              List A = new List(new int\[\] { 7, 8, 9 });
      
              List\> lists = new List\>( );
              lists.Add(A);
              lists.Add(B);
              lists.Add(C);
      

      the result would look something like 1,4,7 1,4,8 1,4,9 2,4,7 ..... any help or direction would be greatly appriciated :)

      modified on Monday, October 5, 2009 1:35 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      cechode wrote:

      a lambada expression

      you'll need quite a swing to pull this one off. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      C 1 Reply Last reply
      0
      • L Luc Pattyn

        cechode wrote:

        a lambada expression

        you'll need quite a swing to pull this one off. :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        C Offline
        C Offline
        cechode
        wrote on last edited by
        #3

        i've been swingin and missing for a bit now. now trying to somehow get

        among the things i've tried so far
        tried to convert this to a self calling kinda thing
        var PP = A.Where(x = > x > 0).SelectMany(g = > B.Where(c = > c > 0).Select(c = > new { aa = g, bb = c })).ToList();

        but to no avail

        then tried to get a bunch of from clauses ( one for each inner list )

                var ttt = (from g in A
                          from c in B
                          select new { aa = g, bb = c }).ToList();
        

        but also failed

        now i figured i'd ask the experts here

        I 1 Reply Last reply
        0
        • C cechode

          i've been swingin and missing for a bit now. now trying to somehow get

          among the things i've tried so far
          tried to convert this to a self calling kinda thing
          var PP = A.Where(x = > x > 0).SelectMany(g = > B.Where(c = > c > 0).Select(c = > new { aa = g, bb = c })).ToList();

          but to no avail

          then tried to get a bunch of from clauses ( one for each inner list )

                  var ttt = (from g in A
                            from c in B
                            select new { aa = g, bb = c }).ToList();
          

          but also failed

          now i figured i'd ask the experts here

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          Ok... As a warning, my brain is a little lopsided today... Here's the first weird idea that popped into my head... Partly in pseudocode, and haven't tried it out, but here ya go...

          private IEnumerable<List<int>> Test(List<List<int>> data)
          {
          int numDigits = data.Count;
          int lastCount = data[numDigits - 1].Count;
          int[] indices = new int[data.Count];
          while (indices[numDigits - 1] < lastCount)
          {
          //yield return a list composed of data[0][indices[0]], data[1][indices[1]], ...

                indices\[0\]++;
                for (int idx = 0; idx < numDigits - 1; idx++)
                if (indices\[idx\] == data\[idx\].Count)
                {
                    indices\[idx\] = 0;
                    indices\[idx + 1\]++;
                }
            }
          

          }

          Bit of a different angle from what you're trying... Instead of going for recursion, think of it like a series of digits. What this really does is count through the following sequence (Assuming all of your arrays have 3 digits): 000, 100, 200, 010, 110, 210, 020, 120, 220, 001, 101, 201... etc And it forms results such that, for example, 120 = the second, third, and first digits of the elements, respectively... Anyway, you get the idea. If you know for a fact that you'll be dealing with 3-digit inner lists, you can simplify this a bit...

          Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups