1

I have a set of data in a list eg

  • john

  • james

  • Smith

  • j

  • s

from this I need all possible combinations and orders eg

  • j s James

  • s j James

  • smith j james

  • j james

  • s j

the list size is not preset and is based off data entered by the user

I have reviewed several solutions on here and have come close to getting what I need for example I get a return of

  • j s james

  • s j james

  • smith j james

but I will never get a result that starts with james

some of the solutions provided in below links Listing all permutations of a string/integer Creating a power set of a Sequence All Possible Combinations of a list of Values

public static IList<IList<T>> PowerSet<T>(IList<T> list)
{       
    int n = 1 << list.Count;
    IList<IList<T>> powerset = new List<IList<T>>();
    for (int i = 0; i < n; ++i)
    {
        IList<T> set = new List<T>();
        for (int bits = i, j = 0; bits != 0; bits >>= 1, ++j)
        {
            if ((bits & 1) != 0)
                set.Add(list[j]);
        }
        powerset.Add(set);
    }
    return powerset;
}

Throwing a list at the above code gets the closest i can get but does not return all the possible combinations

I got this code from here but ill be dammed if i can find the original post all credit to original poster not me for above code

Ness
  • 21
  • 5
  • Please add an [example](https://stackoverflow.com/help/minimal-reproducible-example): a working piece code for what you tried and did not work, including the input data, and what you would like the result to be. – Hans Kapitein Jun 26 '19 at 07:58
  • To clarify the question - the POWER SET of a set of objects is the collection of all subsets of the original set, ignoring ordering. This is what two of your links talk about and the code you quote does. What you want is the set of permutations of the original set, which is a different beast. – Julia Hayward Jun 26 '19 at 08:38
  • 1
    The correct answer is here: https://stackoverflow.com/questions/1952153/what-is-the-best-way-to-find-all-combinations-of-items-in-an-array/10629938#10629938 – Julia Hayward Jun 26 '19 at 08:40
  • @JuliaHayward thanks for the above I realized I was not 100% clear and have also edited the question i also need sets where not all strings are used. know this is going to be an extremely large data set when done. I used the example by Guffa in the link you posted but cannot get the smaller sets. Any advice on this? – Ness Jul 02 '19 at 07:21
  • 1
    OK, so you want all permutations of all possible subsets of the original set? That's going to be large - but you can do it by generating the power set, then feeding each subset into the permutation method in turn, and combining all the outputs. – Julia Hayward Jul 02 '19 at 08:53

0 Answers0