Find All Possible Permutations In Certain Range C# -
i want make program find valid stock symbols yahoo finance , found this: quickest way enumerate alphabet however, wan't go - z , aa - az , aba - abz , on , forth. best way this? more clear example: b c d ect. aa ab ac ad ect. aba abb abc abd ect.
using eric lippert's cartesian product,
var chars = "abcdefghijklmnopqrstuvwxyz"; int maxlen = 3; var query = enumerable.range(1, maxlen) .selectmany(i => enumerable.repeat(chars, i) .cartesianproduct() .select(x => string.concat(x))); foreach(var str in query) { console.writeline(str); } ps: completeness:
public static ienumerable<ienumerable<t>> cartesianproduct<t>(this ienumerable<ienumerable<t>> sequences) { // base case: ienumerable<ienumerable<t>> result = new[] { enumerable.empty<t>() }; foreach (var sequence in sequences) { var s = sequence; // don't close on loop variable // recursive case: use selectmany build new product out of old 1 result = seq in result item in s select seq.concat(new[] { item }); } return result; }
Comments
Post a Comment