我正在尝试在C#中创建一个算法,它产生以下输出字符串:
AAAA AAAB AAAC ...and so on... ZZZX ZZZY ZZZZ
完成此任务的最佳方法是什么?
public static IEnumerableGetWords() { //Perform algorithm yield return word; }
angry person.. 17
好吧,如果长度是一个常数4,那么这将处理它:
public static IEnumerableGetWords() { for (Char c1 = 'A'; c1 <= 'Z'; c1++) { for (Char c2 = 'A'; c2 <= 'Z'; c2++) { for (Char c3 = 'A'; c3 <= 'Z'; c3++) { for (Char c4 = 'A'; c4 <= 'Z'; c4++) { yield return "" + c1 + c2 + c3 + c4; } } } } }
如果长度是一个参数,这个递归解决方案将处理它:
public static IEnumerableGetWords(Int32 length) { if (length <= 0) yield break; for (Char c = 'A'; c <= 'Z'; c++) { if (length > 1) { foreach (String restWord in GetWords(length - 1)) yield return c + restWord; } else yield return "" + c; } }
Garry Shutle.. 15
总是有强制性的LINQ实现.最有可能是垃圾性能,但是从什么时候开始使用酷炫的新功能呢?
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); var sequence = from one in letters from two in letters from three in letters from four in letters orderby one, two, three, four select new string(new[] { one, two, three, four });
'sequence'现在是一个包含AAAA到ZZZZ的IQueryable.
编辑:
好的,所以我觉得应该可以通过LINQ使用可配置的字母表来创建一个可配置长度序列.所以这就是.再一次,完全没有意义,但这让我烦恼.
public void Nonsense() { var letters = new[]{"A","B","C","D","E","F", "G","H","I","J","K","L", "M","N","O","P","Q","R","S", "T","U","V","W","X","Y","Z"}; foreach (var val in Sequence(letters, 4)) Console.WriteLine(val); } private IQueryableSequence(string[] alphabet, int size) { // create the first level var sequence = alphabet.AsQueryable(); // add each subsequent level for (var i = 1; i < size; i++) sequence = AddLevel(sequence, alphabet); return from value in sequence orderby value select value; } private IQueryable AddLevel(IQueryable current, string[] characters) { return from one in current from character in characters select one + character; }
对Sequence方法的调用产生与之前相同的AAAA到ZZZZ列表,但现在您可以更改使用的字典以及生成的字词的长度.
好吧,如果长度是一个常数4,那么这将处理它:
public static IEnumerableGetWords() { for (Char c1 = 'A'; c1 <= 'Z'; c1++) { for (Char c2 = 'A'; c2 <= 'Z'; c2++) { for (Char c3 = 'A'; c3 <= 'Z'; c3++) { for (Char c4 = 'A'; c4 <= 'Z'; c4++) { yield return "" + c1 + c2 + c3 + c4; } } } } }
如果长度是一个参数,这个递归解决方案将处理它:
public static IEnumerableGetWords(Int32 length) { if (length <= 0) yield break; for (Char c = 'A'; c <= 'Z'; c++) { if (length > 1) { foreach (String restWord in GetWords(length - 1)) yield return c + restWord; } else yield return "" + c; } }
总是有强制性的LINQ实现.最有可能是垃圾性能,但是从什么时候开始使用酷炫的新功能呢?
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); var sequence = from one in letters from two in letters from three in letters from four in letters orderby one, two, three, four select new string(new[] { one, two, three, four });
'sequence'现在是一个包含AAAA到ZZZZ的IQueryable.
编辑:
好的,所以我觉得应该可以通过LINQ使用可配置的字母表来创建一个可配置长度序列.所以这就是.再一次,完全没有意义,但这让我烦恼.
public void Nonsense() { var letters = new[]{"A","B","C","D","E","F", "G","H","I","J","K","L", "M","N","O","P","Q","R","S", "T","U","V","W","X","Y","Z"}; foreach (var val in Sequence(letters, 4)) Console.WriteLine(val); } private IQueryableSequence(string[] alphabet, int size) { // create the first level var sequence = alphabet.AsQueryable(); // add each subsequent level for (var i = 1; i < size; i++) sequence = AddLevel(sequence, alphabet); return from value in sequence orderby value select value; } private IQueryable AddLevel(IQueryable current, string[] characters) { return from one in current from character in characters select one + character; }
对Sequence方法的调用产生与之前相同的AAAA到ZZZZ列表,但现在您可以更改使用的字典以及生成的字词的长度.
只是对Garry Shutler的讽刺,但我想要代码着色:
你真的不需要使它成为IQuaryable,也不需要排序,所以你可以删除第二种方法.一步转发是使用Aggregate进行交叉产品,它最终会像这样:
IEnumerableletters = new[]{ "A","B","C","D","E","F", "G","H","I","J","K","L", "M","N","O","P","Q","R","S", "T","U","V","W","X","Y","Z"}; var result = Enumerable.Range(0, 4) .Aggregate(letters, (curr, i) => curr.SelectMany(s => letters, (s, c) => s + c)); foreach (var val in result) Console.WriteLine(val);
安德斯应该获得Linq的诺贝尔奖!