Long story short I am trying to create some sort of chess library that would work potentially for chess boards of any size up to, maybe, int.MaxValue
by int.MaxValue
. Generating an infinite list of rank number is easy enough:
static IEnumerable<int> GetRankNumbers()
{
for(int i = 1; ; i++)
yield return i;
}
But for generating file letters, I am currently doing this:
static IEnumerable<string> GetFileLetters()
{
const string theLetters = "abcdefghjklmnopqrstuvwxyz"; // there is no 'i'.
for(int i = 0; ; i++)
{
var factor = i / theLetters.Length;
var index = i % theLetters.Length;
var sb = new StringBuilder();
sb.Append(theLetters[index]);
for(int j = 0; j < factor; j++)
{
sb.Append(theLetters[index]);
}
yield return sb.ToString();
}
}
Which produces the following:
var x = GetFileLetters().Take(100);
foreach(var item in x) Write(item + " ");
Output:
a b c d e f g h j k l m n o p q r s t u v w x y z aa bb cc dd ee ff gg hh jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz aaa bbb ccc ddd eee fff ggg hhh jjj kkk lll mmm nnn ooo ppp qqq rrr sss ttt uuu vvv www xxx yyy zzz aaaa bbbb cccc dddd eeee ffff gggg hhhh jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz
As you can see, for just a 100 items, it balloons quickly. How can I make it so it returns the results as a, b, c, ..., z, aa, ab, ac, ... , az, ba, bb, .. etc
?
Bonus question: how do I do this in F# as well?
PS. I found some similar questions here, here, and here, but none of them do it with IEnumerable
, yield return
, and a theoretically infinite set like I am trying to. The last one is rather cheating (Coming to think of it I don't really need more .. but I am doing this to learn).