Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created September 18, 2015 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudipto80/ca7c21af763a65dd0aea to your computer and use it in GitHub Desktop.
Save sudipto80/ca7c21af763a65dd0aea to your computer and use it in GitHub Desktop.
Generate all case for all letters in a word using LINQ
void Main()
{
string word = "abcd";
List<List<string>> letters = new List<List<string>>();
letters = word.ToCharArray().Select(w => new List<string>(){w.ToString().ToLower(),w.ToString().ToUpper()} ).ToList();
CartesianProduct(letters)
.Select (x => x.Aggregate ((a,b) => a + b))
.ToList()
.ForEach(Console.WriteLine);
}
static IEnumerable<IEnumerable<T>> CartesianProduct<T>( IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}
// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment