Former colleagues in order to show off the simplicity of Ruby, deliberately out a question to test Xiao Chen:
In writing a burst password dictionary generation tool, which has such a requirement:
Enter a word: Lists all the capitalization combinations for this word, such as Ruby Ruby Ruby Ruby, Ruby, Ruby Ruby .... Wait, this 2^n a
How do you write in C #?
Then he gave the Ruby to Xiao Chen:
s='abcd'[Nil].product (*[s.chars, s.swapcase.chars].transpose). Map (&: Join
Xiao Chen is only a stranger to LINQ. Finally solved the problem with the help of Daniel:
voidMain () {stringWord ="ABCD"; List<List<string>> letters =Newlist<list<string>>(); Letters= Word. ToCharArray (). Select (w =Newlist<string>() {w.tostring (). ToLower (), w.tostring (). ToUpper ()}). ToList (); Cartesianproduct (Letters). Select (x= X.aggregate (A, B) + A +b)). ToList (). ForEach (Console.WriteLine);}StaticIenumerable<ienumerable<t>> cartesianproduct<t> (ienumerable<ienumerable<t>>sequences) {IEnumerable<IEnumerable<T>> emptyproduct =New[] {enumerable.empty<t>() }; returnsequences. Aggregate (Emptyproduct, (accumulator, sequence)= fromAccseqinchAccumulator fromIteminchsequenceSelectAccseq. Concat (New[] {item}); }
In Xiao Chen's view is already very concise. But it's still a loser.
Letter Upper Lower Combo