Number of combinations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csfong33@gmail.com

    Number of combinations

    Hi,
    I have arrays below:
    arr1 = { a, b, c}
    arr2 = {1, 2, 3}
    arr3 = {x, y, z}

    I want to get a combination of these array, as below

    arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
    "a3z",
    "b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
    "b3y", "b3z",
    "c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
    "c3y", "c3z"}

    appreciate if someone can help with example algorithm.

    Thanks!

  • Moty Michaely

    #2
    Re: Number of combinations

    On Jun 14, 12:16 pm, csfon...@gmail. com wrote:
    Hi,
    I have arrays below:
    arr1 = { a, b, c}
    arr2 = {1, 2, 3}
    arr3 = {x, y, z}
    >
    I want to get a combination of these array, as below
    >
    arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
    "a3z",
    "b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
    "b3y", "b3z",
    "c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
    "c3y", "c3z"}
    >
    appreciate if someone can help with example algorithm.
    >
    Thanks!
    Hi,

    Maybe:

    string s[] = new string[arr1.Length*arr 2.Length*arr3.L ength];

    for( int i=0; i<arr1.Length; ++i )
    for( int j=0; j<arr1.Length; ++j )
    for( int k=0; k<arr1.Length; ++k )
    s[i+j+k] = String.Concat(s[i], s[j], s[k]);

    Hope this helps.
    Moty

    Comment

    • icemaple

      #3
      Re: Number of combinations

      At Thu, 14 Jun 2007 17:32:50 +0800£¬Moty Michaely <Moty.Mi@gmail. com>
      wrote:
      On Jun 14, 12:16 pm, csfon...@gmail. com wrote:
      >Hi,
      > I have arrays below:
      >arr1 = { a, b, c}
      >arr2 = {1, 2, 3}
      >arr3 = {x, y, z}
      >>
      >I want to get a combination of these array, as below
      >>
      >arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
      >"a3z",
      > "b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
      >"b3y", "b3z",
      > "c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
      >"c3y", "c3z"}
      >>
      >appreciate if someone can help with example algorithm.
      >>
      >Thanks!
      >
      Hi,
      >
      Maybe:
      >
      string s[] = new string[arr1.Length*arr 2.Length*arr3.L ength];
      >
      for( int i=0; i<arr1.Length; ++i )
      for( int j=0; j<arr1.Length; ++j )
      for( int k=0; k<arr1.Length; ++k )
      s[i+j+k] = String.Concat(s[i], s[j], s[k]);
      >
      Hope this helps.
      Moty
      >
      Your code maybe wrong, especially the index of s.
      I think it should be this:

      s[i * arr2.Length * arr3.Length + j * arr3.Length + k] =
      String.Concat(a rr1[i], arr2[j], arr3[k]);

      --
      Best Regards.
      Maple

      Comment

      • Alberto Poblacion

        #4
        Re: Number of combinations


        <csfong33@gmail .comwrote in message
        news:1181812603 .225963.325250@ x35g2000prf.goo glegroups.com.. .
        Hi,
        I have arrays below:
        arr1 = { a, b, c}
        arr2 = {1, 2, 3}
        arr3 = {x, y, z}
        >
        I want to get a combination of these array, as below
        >
        arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
        "a3z",
        "b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
        "b3y", "b3z",
        "c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
        "c3y", "c3z"}
        >
        appreciate if someone can help with example algorithm.
        Three nested "for" loops shoud give the result that you want:

        string[] arr_result = new string[arr1.Length+arr 2.Length+arr3.L ength];
        int i=0;
        foreach (string s1 in arr1)
        foreach (strng s2 in arr2)
        foreach (string s3 in arr3)
        arr_result[i++]=s1+s2+s3;


        Comment

        • Alberto Poblacion

          #5
          Re: Number of combinations

          In a previous message I wrote:
          string[] arr_result = new string[arr1.Length+arr 2.Length+arr3.L ength];
          Ooops... Sorry. It should be arr1.Length*arr 2.Length*arr3.L ength.

          Comment

          • csfong33@gmail.com

            #6
            Re: Number of combinations

            On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
            quitaestoparaco ntes...@poblaci on.orgwrote:
            In a previous message I wrote:
            >
            string[] arr_result = new string[arr1.Length+arr 2.Length+arr3.L ength];
            >
            Ooops... Sorry. It should be arr1.Length*arr 2.Length*arr3.L ength.
            Thanks for the reply. I wonder is there any way without for loop ?
            I might have 4 or more arrays...

            Comment

            • Jeroen

              #7
              Re: Number of combinations

              ....
              Thanks for the reply. I wonder is there any way without for loop ?
              I might have 4 or more arrays...
              Without syntax highlighting or checking the actual solution, I wrote
              the following:

              /*************** *************** *************** ***/
              public static string[] FlattenArrays(S ystem.Array[] arrays)
              {
              if (arrays.Length < 1) throw new System.Exceptio n();

              else if (arrays.Length == 1) return arrays[0];

              else
              {
              string[] recursed =
              FlattenArrays( SomeMethodForSe lectingSublist( arrays, 0, arrays.Length
              - 2) );
              int currentArrayInd ex = arrays.Length-1;

              for (int i = 0; i < arrays[currentArrayInd ex].Length; i++)
              recursed[i] = recursed[i] + arrays[currentArrayInd ex][i];

              return recursed;
              }
              }
              /*************** *************** *************** ***/

              Perhaps if you improve this piece of code you'll get where you want to.

              Comment

              • Fred Mellender

                #8
                Re: Number of combinations

                You want the Cartesian product of the three arrays. For a solution that
                does not depend on nested loops (works for any number of arrays), see
                http://www.frontiernet.net/~fredm/dps/chapter02.doc . The code works with
                Lists, but you could convert it to work with arrays (or convert your arrays
                to Lists).

                The code to take the Cartesian product will take any list of lists and
                return each combination via a "foreach" iterator. So, your calling code
                would be something like:

                List<List<strin g>myArrays; //convert your arrays to lists, put
                them here

                Cartesian<List< string>>answer =
                new Cartesian<List< string>>(myArra ys);

                foreach (List<stringvec tor in answer.cartesia n())
                {
                //do something with the vector

                }


                The code to make the CP is included in the library mentioned in the book.
                It is:

                namespace Search
                {

                public class Cartesian<T>
                {
                List<List<T>toC ross;
                int[] vector;
                public int totalElements = 1;

                public Cartesian(List< List<T>list)
                {

                toCross = list;

                vector = new int[toCross.Count];

                foreach (List<TaList in list)
                totalElements *= aList.Count;
                }

                public IEnumerable<Lis t<T>cartesian( )
                {
                reset();
                while (true)
                {
                yield return makeList();
                int startAt = toCross.Count - 1;
                while (vector[startAt] == toCross[startAt].Count - 1)
                {
                vector[startAt] = 0;
                startAt--;
                if (startAt < 0)
                break;
                }
                if (startAt < 0)
                break;

                vector[startAt]++;
                }
                }

                List<TmakeList( )
                {
                List<Tans = new List<T>
                (toCross.Count) ;

                for (int k = 0; k < vector.Length; k++)
                {
                ans.Add((toCros s[k])[vector[k]]);
                }
                return ans;
                }

                public void reset()
                {
                for (int i = 0; i < toCross.Count; i++)
                {
                vector[i] = 0;
                }
                }
                }
                }


                <csfong33@gmail .comwrote in message
                news:1181812603 .225963.325250@ x35g2000prf.goo glegroups.com.. .
                Hi,
                I have arrays below:
                arr1 = { a, b, c}
                arr2 = {1, 2, 3}
                arr3 = {x, y, z}
                >
                I want to get a combination of these array, as below
                >
                arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
                "a3z",
                "b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
                "b3y", "b3z",
                "c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
                "c3y", "c3z"}
                >
                appreciate if someone can help with example algorithm.
                >
                Thanks!
                >

                Comment

                • Ignacio Machin \( .NET/ C# MVP \)

                  #9
                  Re: Number of combinations

                  This sounds like homework :)

                  <csfong33@gmail .comwrote in message
                  news:1181812603 .225963.325250@ x35g2000prf.goo glegroups.com.. .
                  Hi,
                  I have arrays below:
                  arr1 = { a, b, c}
                  arr2 = {1, 2, 3}
                  arr3 = {x, y, z}
                  >
                  I want to get a combination of these array, as below
                  >
                  arr_result = { "a1x", "a1y", "a1z", "a2x", "a2y", "a2z", "a3x", "a3y",
                  "a3z",
                  "b1x", "b1y", "b1z", "b2x", "b2y", "b2z", "b3x",
                  "b3y", "b3z",
                  "c1x", "c1y", "c1z", "c2x", "c2y", "c2z", "c3x",
                  "c3y", "c3z"}
                  >
                  appreciate if someone can help with example algorithm.
                  >
                  Thanks!
                  >

                  Comment

                  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                    #10
                    Re: Number of combinations

                    csfong33@gmail. com wrote:
                    On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
                    quitaestoparaco ntes...@poblaci on.orgwrote:
                    >In a previous message I wrote:
                    >>string[] arr_result = new string[arr1.Length+arr 2.Length+arr3.L ength];
                    > Ooops... Sorry. It should be arr1.Length*arr 2.Length*arr3.L ength.
                    >
                    Thanks for the reply. I wonder is there any way without for loop ?
                    I might have 4 or more arrays...
                    I made the code below for a similar problem, maybe it could
                    inspire you.

                    Arne

                    =============== =============== ==========

                    using System;
                    using System.Collecti ons.Generic;

                    namespace E
                    {
                    public class Permuting
                    {
                    public delegate void Processor(strin g s);
                    public static void Print(string s)
                    {
                    Console.WriteLi ne(s);
                    }
                    private static void Permute(string prefix, int ix, List<List<Strin g>>
                    data, Processor p)
                    {
                    foreach(string s in data[ix])
                    {
                    if(ix >= data.Count - 1)
                    {
                    p(prefix + s);
                    }
                    else
                    {
                    Permute(prefix + s, ix + 1, data, p);
                    }
                    }
                    }
                    public static void Permute(List<Li st<String>data, Processor p)
                    {
                    Permute("", 0, data, p);
                    }
                    public static void Main(string[] args)
                    {
                    List<List<Strin g>data = new List<List<Strin g>>();
                    data.Add(new List<String>()) ;
                    data.Add(new List<String>()) ;
                    data.Add(new List<String>()) ;
                    data[0].Add("A");
                    data[0].Add("B");
                    data[0].Add("C");
                    data[0].Add("D");
                    data[1].Add("X");
                    data[1].Add("Y");
                    data[1].Add("Z");
                    data[2].Add("1");
                    data[2].Add("2");
                    Permute(data, Print);
                    Console.ReadLin e();
                    }
                    }
                    }

                    Comment

                    • csfong33@gmail.com

                      #11
                      Re: Number of combinations

                      On Jun 15, 8:54 am, Arne Vajhøj <a...@vajhoej.d kwrote:
                      csfon...@gmail. com wrote:
                      On Jun 14, 6:02 pm, "Alberto Poblacion" <earthling-
                      quitaestoparaco ntes...@poblaci on.orgwrote:
                      In a previous message I wrote:
                      >string[] arr_result = new string[arr1.Length+arr 2.Length+arr3.L ength];
                      Ooops... Sorry. It should be arr1.Length*arr 2.Length*arr3.L ength.
                      >
                      Thanks for the reply. I wonder is there any way without for loop ?
                      I might have 4 or more arrays...
                      >
                      I made the code below for a similar problem, maybe it could
                      inspire you.
                      >
                      Arne
                      >
                      =============== =============== ==========
                      >
                      using System;
                      using System.Collecti ons.Generic;
                      >
                      namespace E
                      {
                      public class Permuting
                      {
                      public delegate void Processor(strin g s);
                      public static void Print(string s)
                      {
                      Console.WriteLi ne(s);
                      }
                      private static void Permute(string prefix, int ix, List<List<Strin g>>
                      data, Processor p)
                      {
                      foreach(string s in data[ix])
                      {
                      if(ix >= data.Count - 1)
                      {
                      p(prefix + s);
                      }
                      else
                      {
                      Permute(prefix + s, ix + 1, data,p);
                      }
                      }
                      }
                      public static void Permute(List<Li st<String>data, Processor p)
                      {
                      Permute("", 0, data, p);
                      }
                      public static void Main(string[] args)
                      {
                      List<List<Strin g>data = new List<List<Strin g>>();
                      data.Add(new List<String>()) ;
                      data.Add(new List<String>()) ;
                      data.Add(new List<String>()) ;
                      data[0].Add("A");
                      data[0].Add("B");
                      data[0].Add("C");
                      data[0].Add("D");
                      data[1].Add("X");
                      data[1].Add("Y");
                      data[1].Add("Z");
                      data[2].Add("1");
                      data[2].Add("2");
                      Permute(data, Print);
                      Console.ReadLin e();
                      }
                      }
                      >
                      >
                      >
                      }- Hide quoted text -
                      >
                      - Show quoted text -

                      Thanks for all the help!


                      Comment

                      Working...