Created
September 18, 2015 02:52
-
-
Save sudipto80/ca7c21af763a65dd0aea to your computer and use it in GitHub Desktop.
Generate all case for all letters in a word using LINQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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