How do I create a bunch of alphabetic strings so that the first is "a", the 26th is "z", 27th is "aa"....e.g.:
a
b
c
d
...
...
...
x
y
z
aa
ab
ac
ad
...
...
...
ax
ay
az
aaa
aab
aac
...
...
...
aax
aay
aaz
aaaa
aaab
...
...
I have the following playground code, but number 27 is "za" (s/b "aa"). I want to ultimately have the ability to create an infinite-length string w/ that pattern.
Below is a rewrite of your function that should do the trick. You'll note that is uses the fact that the letters A-Z have a sequential unicode numbering, meaning the alphabet does not have to be stored.
func NextAlias(last string) string {
if last == "" {
return "a"
} else if last[len(last)-1] == 'z' {
return last[:len(last)-1] + "aa"
} else {
return last[:len(last)-1] + string(last[len(last)-1] + 1)
}
}
Rather than making use of your code I just re-wrote a little simpler algorithm to do this; https://play.golang.org/p/Iv6X8-SXY3
package main
import "fmt"
func main() {
letters := []string{"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"}
prefix := "a"
for i := 0; i < 5; i++ {
for i, _ := range letters {
letters = append(letters, prefix+letters[i%26])
}
prefix += "a"
}
for j, _ := range letters {
fmt.Println(letters[j])
}
}
You could also expand this so there is a variant prefix to get things like ba, bc, bd, bba, ect. To do that you'd want to change the outer loop to make use of the existing items in letters rather than statically calculating a prefix made only of a's. To increase the length of the strings just make i
larger on the outer loop. You could also wrap this in a function and make that an argument to it. Lastly, if performance is a concern I recommend making your slice with a large initial capacity because this is making liberal use of append which will cause a lot or realloc's.