1

In short I am looking for a way in Java/C# for generation of all possible variations based on different sets containing different values. For example: Lets say we have set of names:

 -Max, Jack, Roger

Second set of verbs

-Loves, hates, knows

And third set of programming languages just as an example but we may have 10 sets

-Java, C#, Python, Visual Basic, C++

What I want is a way to generate all possible variations containing ALL attributes and having all values for example an output should be :

Max loves Java
Jack loves Java
Roger loves Java
Max hates Java
Jack hates Java
Roger hates Java
Max knows Java
Jack knows Java
Roger knows Java
Max loves C#
Jack loves C# 
Roger loves C#
and so on... this will generate 45 variations if I am not wrong at the end

Can anyone help ? I believe a similar easier example will be if you want to generate a variations of products in some clothing shop which have different sizes, colors and materials for example and you want all variations.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jNayden
  • 1,592
  • 2
  • 17
  • 29
  • 1
    http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx and links seq. – AakashM Jun 06 '13 at 15:34
  • 3
    Lots on SO when you search Cartesian Product; pick your favourite dupe :) – AakashM Jun 06 '13 at 15:35
  • 5
    Simply loop through each set and append the strings? e.g, something like `for i in (Max, Jack, Roger) for j in (loves, hates) for k in (Java, C#) print i+j+k;`? – Hari Menon Jun 06 '13 at 15:37

1 Answers1

4
String[] names={"Max", "Jack", "Roger"};
String[] verbs={"Loves", "hates", "knows"};
String[] languages={"Java", "C#", "Python", "Visual Basic", "C++"};
for(String name:names)
   for(String verb:verbs)
      for(String language:languages)
         System.out.println(name+" "+verb+" "+language);

EDIT:

Oh ,i see . I misunderstood the question . You can just use the magic of recursion to solve this. This is what you want:

public static void main(String[] args) {
    String[][] sets={
            {"Max", "Jack", "Roger"},
            {"Loves", "hates", "knows"},
            {"Java", "C#", "Python", "Visual Basic", "C++"},
    };
    combine(sets,0,"");
}

public static void combine(String[][] list,int index,String upperText)
{
    if(index==list.length)return;
    for(String i:list[index]){
        combine(list,index+1,upperText+i);
        if(index==list.length-1){
            System.out.println(upperText+i);
        }
    }
}
SteveL
  • 3,331
  • 4
  • 32
  • 57
  • It was a bit more tricky sicne the amount of for loops was not fixed.. I mean the amount of "sets" is not just 3 and I didnt want repercussion ... but anyway I didnt asked correctly so thanks for tha snwer – jNayden Jun 06 '13 at 18:55
  • Yup I agree with recursion you can :) I also found this answer which is in C# but shows good solution as well using the C# magic :) : http://stackoverflow.com/questions/545703/combination-of-listlistint Anyway I've done it already with recursion :) – jNayden Jun 07 '13 at 07:19