I was looking for a way to create a powerset of letters basically to generate any possible letter combination but with a twist that order matters so ab != ba. I ran across a nifty bit of code here which I shamelessly pilfered. Here is my entire program:
using System;
using System.Collections.Generic;
using System.Linq;
namespace PermutationsHelper
{
static class Program
{
static void Main()
{
var ps = GetPowerSet(new List<string>() { "a", "b" });
foreach (var item in ps)
{
string[] resultArr = item.ToArray();
string result = string.Join("", resultArr);
Console.WriteLine(result);
}
Console.ReadLine();
}
static IEnumerable<IEnumerable<T>> GetPowerSet<T>(List<T> list)
{
return from m in Enumerable.Range(0, 1 << list.Count)
select
from i in Enumerable.Range(0, list.Count)
where (m & (1 << i)) != 0
select list[i];
}
}
}
The output is:
[empty]
a
b
ab
But what I am looking for is:
[empty]
a
b
ab
ba
Any suggestions on how best to accomplish this? TIA