如何在c#中初始化一个字符串数组,并将值按顺序从“AAAAAA”到“zzzz”

时间:2022-05-05 13:42:42

I want to easily pre-populate a single dimensional string array which I am calling "letters" with the values:

我想简单地预填充一个我称之为“字母”的一维字符串数组,其值为:

AAAAAA
AAAAAB
AAAAAC
AAAAAD
..
..
ZZZZZX
ZZZZZY
ZZZZZZ

Thats 165 million combinations in order.

这是1.65亿的组合。

The idea being I need to then be able to ask for any particular combination of 6 characters such as BBCHHJ and use Array.Index to return the element of the array it is in.

我的想法是,我需要能够要求任何特定组合的6个字符,如BBCHHJ和使用数组。索引以返回其所在数组的元素。

I have the second bit fine:

第二点很好:

    String searchFor;
    Console.Write("Enter a string value to search for: ");
    searchFor = Console.ReadLine();
    int indexValue = Array.IndexOf(letters, searchFor);

    Console.WriteLine("The value you are after is in element index: " + indexValue);
    Console.ReadLine();

But I have no idea how to easily initialise the letters array with all those combinations, in order!

但是我不知道如何用所有这些组合来初始化字母数组。

3 个解决方案

#1


3  

A variation on Jakub's answer which should be a bit more efficient:

关于Jakub的答案的变化应该更有效率一点:

int result = s
    .Select(c => c - 'A')                              // map 'A'-'Z' to 0-25
    .Aggregate(0, (total, next) => total * 26 + next); // calculate the base 26 value

This has the advantage of avoiding the Reverse and the separate Sum, and the powers of 26 don't have to be calculated from scratch in each iteration.

这样做的好处是避免了反向和单独的和,并且26的幂不必在每次迭代中从头计算。

#2


1  

Storing 308 million elements in array and searching them is not the best solution, rather calculate the index at runtime. I have created a code sample:

在数组中存储3.08亿个元素并搜索它们不是最好的解决方案,而是在运行时计算索引。我创建了一个代码示例:

string input = "ZZZZZZ";

//default values
string alphabets_s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabets = alphabets_s.ToCharArray();

int result = 1; //starting with "one" because zero will make everything zero

//calculating index
for (int i = 0; i < input.Length; i++)
{       
    //get character index and add "1" to avoid multiplication with "0"
    int index = Array.IndexOf(alphabets, input[i]) + 1;

    //multiply it with the current result
    result *= index;
}

//subtract 1 from final result, because we started it with 1
result--;

PS: I did just basic testing, please inform me if you find anything wrong in it.

PS:我只是做了基本的测试,如果发现有问题请通知我。

#3


0  

As I wrote in a comment, what you're trying to achieve is basically conversion from base 26 number.

正如我在一篇评论中所写的,你所要做的基本上就是从26号开始转换。

The first step is to convert the string to a list of digits. Then just multiply by powers of 26 and add together:

第一步是将字符串转换为一组数字。然后乘以26的乘方相加:

var s = "AAAABB";
var result = s
    .Select(c => c - 'A') //map characters to numbers: A -> 0, B -> 1 etc
    .Reverse()            //reverse the sequence to have the least significant digit first
    .Select((d, i) => d * Math.Pow(26, i))
    .Sum();

#1


3  

A variation on Jakub's answer which should be a bit more efficient:

关于Jakub的答案的变化应该更有效率一点:

int result = s
    .Select(c => c - 'A')                              // map 'A'-'Z' to 0-25
    .Aggregate(0, (total, next) => total * 26 + next); // calculate the base 26 value

This has the advantage of avoiding the Reverse and the separate Sum, and the powers of 26 don't have to be calculated from scratch in each iteration.

这样做的好处是避免了反向和单独的和,并且26的幂不必在每次迭代中从头计算。

#2


1  

Storing 308 million elements in array and searching them is not the best solution, rather calculate the index at runtime. I have created a code sample:

在数组中存储3.08亿个元素并搜索它们不是最好的解决方案,而是在运行时计算索引。我创建了一个代码示例:

string input = "ZZZZZZ";

//default values
string alphabets_s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabets = alphabets_s.ToCharArray();

int result = 1; //starting with "one" because zero will make everything zero

//calculating index
for (int i = 0; i < input.Length; i++)
{       
    //get character index and add "1" to avoid multiplication with "0"
    int index = Array.IndexOf(alphabets, input[i]) + 1;

    //multiply it with the current result
    result *= index;
}

//subtract 1 from final result, because we started it with 1
result--;

PS: I did just basic testing, please inform me if you find anything wrong in it.

PS:我只是做了基本的测试,如果发现有问题请通知我。

#3


0  

As I wrote in a comment, what you're trying to achieve is basically conversion from base 26 number.

正如我在一篇评论中所写的,你所要做的基本上就是从26号开始转换。

The first step is to convert the string to a list of digits. Then just multiply by powers of 26 and add together:

第一步是将字符串转换为一组数字。然后乘以26的乘方相加:

var s = "AAAABB";
var result = s
    .Select(c => c - 'A') //map characters to numbers: A -> 0, B -> 1 etc
    .Reverse()            //reverse the sequence to have the least significant digit first
    .Select((d, i) => d * Math.Pow(26, i))
    .Sum();