在特定范围内查找所有可能的排列 C#
Find All Possible Permutations In Certain Range C#
我想编写一个程序从 Yahoo Finance 中查找所有有效的股票代码,我已经找到了这个:Quickest way to enumerate the alphabet
但是,我不希望它从 A - Z 然后是 AA - AZ 然后是 ABA - ABZ 等等。做这个的最好方式是什么?更清楚的例子:A B C D ect。
AA AB AC AD等。 ABA ABB ABC ABD 等等
不确定它有多快,但是当我需要做类似的事情时我做了以下事情:
for (int i = 0; i < numCols && i < 26; i++)
{
char start = 'A';
char colChar = (char)(start + (char)(i));
Console.WriteLine(string.Format("{0}", colChar), typeof(string));
}
for (int i = 26; i < 52 && i < numCols; i++)
{
char start = 'A';
char colChar = (char)(start + (char)(i-26));
Console.WriteLine(string.Format("A{0}", colChar), typeof(string));
}
第二个 for 循环显然只 returns AA 到 AZ,但是如果你把它放在一个函数中,将第一个 A 作为输入,那么你可以通过 A-Z 循环第一个字符,然后你有所有的两个字符结果。使用 2 个字符输入作为前面的字符串创建第三个函数将为您提供三个字符集。
26*26*26 是很多输出,但上面的模式应该适合你。
使用 Eric Lippert 的 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:为了完整起见:
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 over the loop variable
// recursive case: use SelectMany to build the new product out of the old one
result =
from seq in result
from item in s
select seq.Concat(new[] { item });
}
return result;
}
我想编写一个程序从 Yahoo Finance 中查找所有有效的股票代码,我已经找到了这个:Quickest way to enumerate the alphabet 但是,我不希望它从 A - Z 然后是 AA - AZ 然后是 ABA - ABZ 等等。做这个的最好方式是什么?更清楚的例子:A B C D ect。 AA AB AC AD等。 ABA ABB ABC ABD 等等
不确定它有多快,但是当我需要做类似的事情时我做了以下事情:
for (int i = 0; i < numCols && i < 26; i++)
{
char start = 'A';
char colChar = (char)(start + (char)(i));
Console.WriteLine(string.Format("{0}", colChar), typeof(string));
}
for (int i = 26; i < 52 && i < numCols; i++)
{
char start = 'A';
char colChar = (char)(start + (char)(i-26));
Console.WriteLine(string.Format("A{0}", colChar), typeof(string));
}
第二个 for 循环显然只 returns AA 到 AZ,但是如果你把它放在一个函数中,将第一个 A 作为输入,那么你可以通过 A-Z 循环第一个字符,然后你有所有的两个字符结果。使用 2 个字符输入作为前面的字符串创建第三个函数将为您提供三个字符集。
26*26*26 是很多输出,但上面的模式应该适合你。
使用 Eric Lippert 的 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:为了完整起见:
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 over the loop variable
// recursive case: use SelectMany to build the new product out of the old one
result =
from seq in result
from item in s
select seq.Concat(new[] { item });
}
return result;
}