0

I am running into a peculiar problem of which i couldn't find any solution after long search. Here is my question:

I have two String Arrays:

   var arr1 = new[] { "UserId", "BookId", "BookStoreId", "Appid" };
   var arr2 = new[] {"Same", "Unique", "Repetitive"};

I want Result with all possible combinations like:

Result (Should be in tabular format):

Columns- UserId BookId BookStoreId Appid

  • Row1 Same Same Same Same
  • Row2 Same Same Same Unique
  • Row3 Same Same Same Repetitive

  • Row4 Same Same Unique Same

  • Row5 Same Same Repetitive Same

  • Row6 Same Same Unique Unique

  • Row6 Same Same Unique Repetitive
  • Row7 Same Same Repetitive Unique
  • Row8 Same Same Repetitive Repetitive

  • Row9 Same Unique Same Same

  • Row10 Same Unique Unique Same
  • Row11 Same Repetitive Same Same and so on...

Rows and Columns are only for notation purpose.

I hope my question is clear now.

I Should create 64 Unique Combinations.

I have tried lots of ways including Cartesian products but that didnt solved my problem.

nks
  • 61
  • 2
  • 12
  • how about [this post](https://stackoverflow.com/questions/1328079/calculate-all-possible-pairs-of-items-from-two-lists) ? – Mong Zhu Jan 24 '19 at 14:43
  • 1
    So you want all possible combinations with length 4 of items in `arr2`? See [permutation with repetions](https://stackoverflow.com/a/10629938/1997232). – Sinatr Jan 24 '19 at 14:49
  • @MongZhu This doesn't give expected output, I want result in table format with ColumnNames as arr1 values and Rows as Arr2 Values. – nks Jan 24 '19 at 14:49
  • You are counting : 0000,0001,0002,0003, 0010, 0011, 0012,0013,0020,0021,0022,0023,0030,0031,0032,0033,0100,0101,0102,0103,... – jdweng Jan 24 '19 at 14:50
  • @Sinatr That solution doesnt fit my question,That solution is about permutations of single array elements within them, not what i am looking for. – nks Jan 24 '19 at 14:52
  • 2
    "I Should create 64 Unique Combinations." I don't see why it would only be 64. Each of the elements can be any of the 7 strings, right? So there should be 7^4=2401 combinations. Your question isn't very clear at the moment... (It's also not really obvious why your first example uses everything from `arr1`, but all your others only use items from `arr2`. What's the relevance of that?) – Jon Skeet Jan 24 '19 at 14:57
  • @JonSkeet , Consider arr1 elements as column names and arr2 elements as their values,keeping the column names static we need to swap values dynamically for all possible unique combinations with repetitions. Consider the resultset as a table(it might not be visible like that), see my edited question. – nks Jan 25 '19 at 02:44
  • 1
    So actually the first array is irrelevant in terms of coming up with combinations? Or is it just that that provides the *length* of the combinations you need? (If so, it would be helpful not to bother even showing the string array - you basically want a method accepting a string array of options, and the number of items per row.) – Jon Skeet Jan 25 '19 at 06:50

1 Answers1

3

Seems to you need a Combinations With Repetitions. Check this answer https://stackoverflow.com/a/25824818/1007620

Summary:

static IEnumerable<String> CombinationsWithRepition(IEnumerable<string> input, int length)
{
    if (length <= 0)
        yield return "";
    else
    {
        foreach(var i in input)
            foreach(var c in CombinationsWithRepition(input, length-1))
                yield return i + ',' + c;
    }
}

Then

string[] items = {"UserId", "BookId", "BookStoreId", "Appid", "Same", "Unique", "Repetitive"};
foreach (var c in CombinationsWithRepition(items, 4))
    Console.WriteLine (c);

Result https://pastebin.com/2hCbA1XJ

brevis
  • 1,191
  • 10
  • 15
  • Not what i expect, Consider arr1 elements as column names and arr2 elements as their values,keeping the column names static we need to swap values dynamically for all possible unique combinations with repetitions. Consider the resultset as a table(it might not be visible like that), see my edited question – nks Jan 25 '19 at 02:44
  • 1
    then use the same code and remove `arr1` elements out of the items, you'll get 3^4 = 81 combinations – Kien Chu Jan 25 '19 at 02:59
  • @KienChu Thanks, It worked well, i was overthinking of arr1 it seems... – nks Jan 25 '19 at 03:57