3

I have a Dictionary<string, HashSet<string>> that I would like to go through and find all the combinations in the HashSet possible without any repeats. For example (dummy data):

Keys          Values (HashSet)

"greeting"     "Hello" "Hi" "Hey" "Howdy" ...
"Name"         "Tom" "Angel" "Edward" ...
"question"     "how are you?" "can you help me?" "is everything okay?" ...
    ...                  ...
    ...                ....

I would like the output to be some sort of collection with each value as:

"Hello Tom how are you?"
"Hello Tom can you help me?"
"Hello Tom is everything okay?"
"Hello Angel how are you?"
"Hello Angel can you help me?"
"Hello Angel is everything okay?"
"Hello Edward how are you?"
...

"Hi Tom how are you?"
"Hi Tom can you help me?"
"Hi Tom is everything okay?"
"Hi Angel how are you?"
...

I would like to have this as abstract as possible to where I can add as many keys and values in the hashset as I want.

I have tried to do it recursively but I am not too strong with that and I cannot come up with a base case... I think you can do this with linq but I am not familiar with linq at all.

The reason I am using a Dictionary<string, HashSet<string>> is because of how I am gathering the information so I would like to keep this data structure.

Thank you!

user3369494
  • 123
  • 11
  • Search "permutations". – CodeCaster Apr 13 '15 at 15:04
  • possible duplicate of [Generating all Possible Combinations](http://stackoverflow.com/questions/3093622/generating-all-possible-combinations) – CoderDennis Apr 13 '15 at 15:04
  • @CoderDennis: that's not exactly what I want, I want to do this with a dictionary of hashsets which I am confused on how to approach this. I also would like to get the first value of hashset of first key with the first value of hashet of second key and so forth til i reach the last hashset then do it all over again except with the second value of the last hashet.... I don't know if that makes any sense – user3369494 Apr 13 '15 at 15:19
  • 1
    That is exactly what the answers to that question show. Your output doesn't use the key, so that doesn't change anything. Thinking only of your values, you've essentially got `IEnumerable>`. – CoderDennis Apr 13 '15 at 15:24

2 Answers2

3

Given the example data provided in the question:

var data = new Dictionary<string, HashSet<string>>
{
    {"greeting", new HashSet<string> {"Hello", "Hi", "Hey", "Howdy"}},
    {"Name", new HashSet<string> {"Tom", "Angel", "Edward"}},
    {"question", new HashSet<string> {"how are you?", "can you help me?", "is everything okay?"}}
};

Without arbitrary keys, here's an easy way to do it with LINQ:

var collection = from g in data["greeting"]
    from n in data["Name"]
    from q in data["question"]
    select string.Format("{0} {1} {2}", g, n, q);

Using the CartesianProduct extension method from this answer would look like this:

var collection = data.Select(x => x.Value).CartesianProduct().Select(x => x.Aggregate((a, b) => a + " " + b));

In either case, here's the foreach I used to display the output in a console app:

foreach (var line in collection)
{
    Console.WriteLine(line);
}
Community
  • 1
  • 1
CoderDennis
  • 13,642
  • 9
  • 69
  • 105
0

A simple solution would be...

Dictionary<string, HashSet<string>> test = new Dictionary<string, HashSet<string>>();
        test.Keys.ToList().ForEach(key =>
        {
            test[key].ToList().ForEach(value => Console.WriteLine("key key:" + "value:" + value));
        });
weismat
  • 7,195
  • 3
  • 43
  • 58
  • that's not exactly what I want, I don't want to just print the values of the hashset associated with the key, I want to print all the combinations of the values of the hashset across all the keys. Like the first value of hashset of first key with the first value of hashet of second key, etc. – user3369494 Apr 13 '15 at 15:18
  • You should provide more code. You would need to define a list with the values and order in which you want to permute than. – weismat Apr 13 '15 at 15:21