0 Replies - 578 Views - Last Post: 26 April 2013 - 10:46 AM Rate Topic: -----

#1 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

How to get all possible combinations of strings from List<string[]

Posted 26 April 2013 - 10:46 AM

I'm doing a tag-like string match function where function checks is string contains any of possible words while maintaining their order, at least per tag.
I figured out it will be best to precreate list of possibilities and on check simply see if string contains each of required combinations

> pattern is splitted by ','
> split parts called tags are further splitted by ' '
> split parts called VarWord are further splitted by '|' (i actually don't name them in code)

check should see if text contains all tags, where each tag technically appears in many forms since it contains words which may vary.
So: "a|b c|d, e|f g|h"
a c f h = is ok
b c e g = is ok
c a f h = is wrong since c and a are swapped, a|b should be before c|d
a c h f = is wrong since f and h are swapped, e|f should be before g|h

Maybe code will make it more clear.
    List<List<string[]>> tags;

    List<string[]> innerList;

    List<List<string>> combinationsList;

    public void Generate(string pattern)
        // i will add whitespace removal later so it can be ", " instead of only ","

        foreach (string tag in pattern.Split(','))
            innerList = new List<string[]>();

            foreach (var varyword in tag.Split(' '))

        // atm i lack code to generate combinations in form of List<List<string>> 
        // and drop them into 'combinationsList'
    // the check function will look something like isMatch = :
    public bool IsMatch(string textToTest)
        return combinationsList.All(tc => tc.Any(c => textToTest.Contains(c)));

So for example pattern:

"old|young john|bob, have|posses dog|cat"

  • tags
    • List_1:
      • {old, young}
      • {john, bob}

    • List_2
      • {have, posses}
      • {dog, cat}

So combinationsList will have:

  • combinationsList :
    • List_1
      • "old john"
      • "old bob"
      • "young john"
      • "young bob"

    • List_2
      • "have dog"
      • "have cat"
      • "posses dog"
      • "posses cat"

So results will be:

* old bob have cat = true, contains List_1:"old bob" and List_2:"have cat"
* young john have car = false, contains List_1:"young john" but doesn't contain any of List_2 combinations

I cannot figure out how to iterate collection to get those combinations and how to get the combination per iteration.
Also i cannot mess up the order so old john will not be also generated as john old.

Please note that any of "variant words" in pattern may have more than 2 variants like for example "dog|cat|mouse"

This post has been edited by Nerfpl: 26 April 2013 - 10:54 AM

Is This A Good Question/Topic? 0
  • +

Page 1 of 1