I am trying to create a method that will return all subsets of a set.
For example if I have the collection 10,20,30
I will like to get the following ou
A simple algorithm based upon recursion:
private static List> GetPowerList(List a)
{
int n = a.Count;
var sublists = new List>() { new List() };
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
var first = a[i];
var last = a[j];
if ((j - i) > 1)
{
sublists.AddRange(GetPowerList(a
.GetRange(i + 1, j - i - 1))
.Select(l => l
.Prepend(first)
.Append(last).ToList()));
}
else sublists.Add(a.GetRange(i,j - i + 1));
}
}
return sublists;
}